三、锁
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