synchronize的理解2-使用方法

Synchronized常用三种使用方式

1、修饰普通方法:锁对象即为当前对象

2、修饰代码块:锁对象为synchronized紧接着的小括号内的对象

3、修饰静态方法:锁对象为当前Class对象

1、修饰普通方法

public class MyThread extends Thread{

    private  int count=5;
    @SneakyThrows
    @Override
    public void run() {
        doSomething();
    }

    public synchronized void doSomething(){
        String name = Thread.currentThread().getName();
        System.out.println(name+"开始执行任务");
        try {
            count--;
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(name+"任务结束,count="+count);
    }
    public static void main(String[] args) {
        MyThread myThread=new MyThread();

        Thread threadA = new Thread(myThread,"线程A");
        Thread threadB = new Thread(myThread,"线程B");
        Thread threadC = new Thread(myThread,"线程C");
        Thread threadD = new Thread(myThread,"线程D");
        Thread threadE = new Thread(myThread,"线程E");

        threadA.start();
        threadB.start();
        threadC.start();
        threadD.start();
        threadE.start();

       while (threadA.isAlive()|| threadB.isAlive()|| threadC.isAlive()
               || threadD.isAlive()|| threadE.isAlive()){

        }
        System.out.println("main finish");
    }

}

结果:

 

 

 

2、修饰代码块:锁对象为synchronized紧接着的小括号内的对象

public class MyThread extends Thread{

    private  int count=5;
    @SneakyThrows
    @Override
    public void run() {
        synchronized (this){
            String name = Thread.currentThread().getName();
            System.out.println(name+"开始执行任务");
            try {
                count--;
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(name+"任务结束,count="+count);
        }
    }


    public static void main(String[] args) {
        MyThread myThread=new MyThread();

        Thread threadA = new Thread(myThread,"线程A");
        Thread threadB = new Thread(myThread,"线程B");
        Thread threadC = new Thread(myThread,"线程C");
        Thread threadD = new Thread(myThread,"线程D");
        Thread threadE = new Thread(myThread,"线程E");

        threadA.start();
        threadB.start();
        threadC.start();
        threadD.start();
        threadE.start();

       while (threadA.isAlive()|| threadB.isAlive()|| threadC.isAlive()
               || threadD.isAlive()|| threadE.isAlive()){

        }
        System.out.println("main finish");
    }

}

 

3、修饰静态方法:锁对象为当前Class对象

因为 static 方法在类加载的时候就加载了,所以这个锁应该是类的字节码对象。

synchronized 关键字修饰 static 方法的时候,同步锁是该类的字节码对象,即等效于代码块 synchronized(classname.class){...}。

public class MyThread extends Thread{


        private static int count=5;
        @SneakyThrows
        @Override
        public void run() {
           doSomething();
        }

        public synchronized static void doSomething(){
            String name = Thread.currentThread().getName();
            System.out.println(name+"开始执行任务");
            try {
                count--;
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(name+"任务结束,count="+count);
        }

        public static void main(String[] args) {
            MyThread myThread=new MyThread();

            Thread threadA = new Thread(myThread,"线程A");
            Thread threadB = new Thread(myThread,"线程B");
            Thread threadC = new Thread(myThread,"线程C");
            Thread threadD = new Thread(myThread,"线程D");
            Thread threadE = new Thread(myThread,"线程E");

            threadA.start();
            threadB.start();
            threadC.start();
            threadD.start();
            threadE.start();

            while (threadA.isAlive()|| threadB.isAlive()|| threadC.isAlive()
                    || threadD.isAlive()|| threadE.isAlive()){

            }
            System.out.println("main finish");
        }


}

结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值