java使用synchronized加锁

synchronized的作用:同步代码块

不使用synchronized

public class MyRunnable implements Runnable{

    private Integer id = 0;

    @Override
    public void run() {
            for (int i = 0; i < 100; i++) {
                System.out.println("我是 "+Thread.currentThread().getName()+" 的id: "+(++id));
            }
    }

}
public static void main(String[] args) {
    MyRunnable myRunnable = new MyRunnable();
    for (int i = 0; i < 50; i++) {
        new Thread(myRunnable).start();
    }
}

结果:

...
我是 Thread-10 的id: 4983
我是 Thread-10 的id: 4984
我是 Thread-10 的id: 4985
我是 Thread-10 的id: 4986
我是 Thread-10 的id: 4987

Process finished with exit code 0

使用synchronized

  • 方式一:

    public class MyRunnable implements Runnable{
    
        private Integer id = 0;
    
        @Override
        public synchronized void run() {
                for (int i = 0; i < 100; i++) {
                    System.out.println("我是 "+Thread.currentThread().getName()+" 的id: "+(++id));
                }
        }
    
    }
    
    public static void main(String[] args) {
        MyRunnable myRunnable = new MyRunnable();
        for (int i = 0; i < 50; i++) {
            new Thread(myRunnable).start();
        }
    }
    

    结果:

    ...
    我是 Thread-15 的id: 4996
    我是 Thread-15 的id: 4997
    我是 Thread-15 的id: 4998
    我是 Thread-15 的id: 4999
    我是 Thread-15 的id: 5000
    
    Process finished with exit code 0
    
  • 方式二:

    public class MyRunnable implements Runnable{
    
        private Integer id = 0;
        private Object obj;
    
        public MyRunnable(Object obj) {
            this.obj = obj;
        }
    
        @Override
        public void run() {
            synchronized (obj){
                for (int i = 0; i < 100; i++) {
                    System.out.println("我是 "+Thread.currentThread().getName()+" 的id: "+(++id));
                }
            }
    
    
        }
    
    }
    
    public static void main(String[] args) {
        Object o = new Object(); //加锁对象
        MyRunnable myRunnable = new MyRunnable(o);
        for (int i = 0; i < 50; i++) {
            new Thread(myRunnable).start();
        }
    }
    

    结果:

    ...
    我是 Thread-10 的id: 4997
    我是 Thread-10 的id: 4998
    我是 Thread-10 的id: 4999
    我是 Thread-10 的id: 5000
    
    Process finished with exit code 0
    
  • 方式三:

    public class MyRunnable implements Runnable{
    
        private Integer id = 0;
        private static Object obj = new Object();
    
        @Override
        public void run() {
            synchronized (obj){
                for (int i = 0; i < 100; i++) {
                    System.out.println("我是 "+Thread.currentThread().getName()+" 的id: "+(++id));
                }
            }
    
        }
    
    }
    
    public static void main(String[] args) {
        MyRunnable myRunnable = new MyRunnable();
        for (int i = 0; i < 50; i++) {
            new Thread(myRunnable).start();
        }
    }
    

    结果:

    ...
    我是 Thread-5 的id: 4996
    我是 Thread-5 的id: 4997
    我是 Thread-5 的id: 4998
    我是 Thread-5 的id: 4999
    我是 Thread-5 的id: 5000
    
    Process finished with exit code 0
    
posted @ 2022-03-07 18:04  叕叕666  阅读(84)  评论(0)    收藏  举报