synchornized学习笔记

1.对象锁示例同步代码块锁 两个线程同时访问一个方法

    static SynchronizedObjectCodeBlock instance = new SynchronizedObjectCodeBlock();

    public static void main(String[] args) {
        Thread thread1 = new Thread(instance);
        Thread thread2 = new Thread(instance);
        thread1.start();
        thread2.start();
        while (thread1.isAlive() || thread2.isAlive()) {

        }
        System.out.println("运行结束");
    }

    @Override
    public void run() {
        synchronized (this) {
            System.out.println("我是对象锁的同步代码块锁" + Thread.currentThread().getName());
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "执行完毕");
        }
    }

输出结果为:

我是对象锁的方法修饰符形式Thread-0
Thread-0执行完毕
我是对象锁的方法修饰符形式Thread-1
Thread-1执行完毕
运行结束

2.对象锁示例普通方法锁 两个线程同时访问一个方法和上边略有不同

 public static void main(String[] args) {
       Thread thread1 = new Thread(instance);
       Thread thread2 = new Thread(instance);
       thread1.start();
       thread2.start();
       while (thread1.isAlive() || thread2.isAlive()) {

       }
       System.out.println("运行结束");
   }

   @Override
   public void run() {
       Method();
   }

   private  synchronized void Method() {
       System.out.println("我是对象锁的方法修饰符形式" + Thread.currentThread().getName());
       try {
           Thread.sleep(2000);
       } catch (InterruptedException e) {
           e.printStackTrace();
       }
       System.out.println(Thread.currentThread().getName() + "执行完毕");
   }

输出结果也是和一输出的一样

我是对象锁的方法修饰符形式Thread-0
Thread-0执行完毕
我是对象锁的方法修饰符形式Thread-1
Thread-1执行完毕
运行结束

3类锁 静态方法锁

 static SynchronizedClassstatic instance1=new SynchronizedClassstatic();
   static SynchronizedClassstatic instance2=new SynchronizedClassstatic();

    public static void main(String[] args) {
        Thread thread1=new Thread(instance1);
        Thread thread2=new Thread(instance2);
        thread1.start();
        thread2.start();
        while (thread1.isAlive() || thread2.isAlive()) {

        }
        System.out.println("运行结束");
    }
    @Override
    public void run() {
        Method();
    }
    private static synchronized void Method() {
        System.out.println("我是类锁的第一种形式静态方法" + Thread.currentThread().getName());
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "执行完毕");
    }

两个线程同时访问了一个静态方法
输出结果为:

我是类锁的第一种形式静态方法Thread-0
Thread-0执行完毕
我是类锁的第一种形式静态方法Thread-1
Thread-1执行完毕
运行结束

可以看到先是Thread0先执行完毕后 Thread1才执行 说明在Thread0没有释放锁的时候Thread1是不能操作的
4类锁的第二种方式 *.class

 static SynchronizedClassClass instance1 = new SynchronizedClassClass();
    static SynchronizedClassClass instance2 = new SynchronizedClassClass();

    public static void main(String[] args) {
        Thread thread1 = new Thread(instance1);
        Thread thread2 = new Thread(instance2);
        thread1.start();
        thread2.start();
        while (thread1.isAlive() || thread2.isAlive()) {

        }
        System.out.println("运行结束");
    }

    @Override
    public void run() {
        Method();
    }

    private void Method() {
        synchronized (SynchronizedClassClass.class) {
            System.out.println("我是对象锁的方法修饰符形式" + Thread.currentThread().getName());
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "执行完毕");
        }
    }

输出结果为:

我是类锁的第一种形式静态方法Thread-0
Thread-0执行完毕
我是类锁的第一种形式静态方法Thread-1
Thread-1执行完毕
运行结束

结果也显示并没有并行操作
总结:
1.一把锁只能被一个线程获取,没有获取的线程只能等待。

2每个实例都对应的有自己的锁,不同实例之间的锁互不影响。(锁对象是*.class和synchornized 修饰的static静态方法时,所有对象是一把锁)

3无论是方法正常执行还是抛出异常都是会释放锁.

缺点
1 效率低 锁的释放时间少 在试图获取锁的时候无法中断和设置超时时间,
2 不灵活 不能设置加锁和释放的时机
3无法知道是否得到锁
注意点
1.锁对象不能为空 .作用域不能过大. 避免死锁

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 练气第28天感悟,感觉左脚没那么麻了,刚开始练气的时候,不仅心里很烦躁而且左脚也麻得厉害,背部也很胀痛,很难受,坚...
    心随意动0808阅读 166评论 0 1
  • “一入缠门深似海” 学缠之前,目光所及只在所处“房间”,现在可以推开窗户,看外面美丽的风景。目光和心所及,看到和...
    待时乘势阅读 225评论 0 2
  • 夏天网络传媒招收女主播.底薪3000一天6小时,底薪2000一天4小时,收入+名气 轻松+愉快
    MC夏天阅读 211评论 0 0
  • 怀揣着梦想,迷茫的走进大学的校门,突然间的惊醒,同学回忆一下你为什么到来。 生活刚开始总会是苦涩的,谁让你选择了向...
    谋云九幻阅读 3,214评论 73 107
  • 恨占据身心 主导所有行动 牙齿咬的咯咯响 躯体不停发抖 不是恐惧 是兴奋 终有一天 复仇之矛会刺入你的心脏 会捣毁...
    沐子韩阅读 332评论 3 4