三、锁

1.synchronized

public class T implements Runnable{
    private int count = 100;
    /**
     * synchronized 既保证了原子性,又保证了可见性
     */
    public /*synchronized*/ void run() { //锁:T的对象 
        count--;
        System.out.println(Thread.currentThread().getName()+",count: "+count);
    }
    public static void main(String[] args){
        T t =  new T();
        for(int i=0; i < 100; i++){
            new Thread(t,"Thread"+i).start();
        }
    }
}

 2.CAS 自旋锁

3.AQS

 

posted @ 2021-03-09 20:50  章冒冒2020  阅读(45)  评论(0编辑  收藏  举报