目录
加锁
cas
信号量
加锁
package org.example;public class ThreadTest implements Runnable{static int i=0;int init=0;public static void main(String []args){ThreadTest t1=new ThreadTest();t1.init=0;ThreadTest t2=new ThreadTest();t2.init=1;Thread a=new Thread(t1);Thread b=new Thread(t2);a.start();b.start();}@Overridepublic void run() {synchronized (ThreadTest.class){while(i<100){if(this.init== i){i+=1;this.init+=2;System.out.println(Thread.currentThread().getName()+" "+i);}else{ThreadTest.class.notifyAll();try {ThreadTest.class.wait();} catch (InterruptedException e) {throw new RuntimeException(e);}}}}}
}