使用synchronized
packagecom.pb.thread.demo5;/**使用synchronized
* 一个线程加一运算,一个线程做减法运算,多个线程同时交替运行
*
*@authorDenny
**/publicclassCount {PRivateintnum = 0;privatebooleanflag =false;//标识//加法publicsynchronizedvoidadd() {while(flag) {try{
wait();
}catch(InterruptedException e) {
e.printStackTrace();
}
}this.num++;//加System.out.println(Thread.currentThread().getName() + "........" +this.num);this.flag=true;//设置标识为truenotifyAll();//唤醒所有在线程池中冻结的线程,会把所有都唤醒}//减法publicsynchronizedvoidsub() {while(!flag) {try{
wait();
}catch(InterruptedException e) {
e.printStackTrace();
}
}this.num--;//减System.out.println(Thread.currentThread().getName() + "........" +this.num);this.flag=false;//设置标识为truenotifyAll();//唤醒所有在线程池中冻结的线程,会把所有都唤醒}
}
packagecom.pb.thread.demo5;publicclassAddimplementsRunnable {privateCount count;publicAdd(Count count){this.count=count;
}
@Overridepublicvoidrun() {while(true){
count.add();
}
}
}//================packagecom.pb.thread.demo5;publicclassSubimplementsRunnable {privateCount count;publicSub(Count count){this.count=count;
}
@Overridepublicvoidrun() {while(true){
count.sub();
}
}
}
测试类
packagecom.pb.thread.demo5;publicclassCountTest {publicstaticvoidmain(String[] args) {
Count c=newCount();
Add add=newAdd(c);
Sub sub=newSub(c);
Thread t1=newThread(add);
Thread t2=newThread(add);
Thread t3=newThread(sub);
Thread t4=newThread(sub);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
结果:
Thread-2........0
Thread-1........1
Thread-3........0
Thread-0........1
Thread-2........0
Thread-1........1
Thread-3........0
Thread-0........1
Thread-2........0
不使用synchronizedpackagecom.pb.thread.demo4;importjava.util.concurrent.locks.Condition;importjava.util.concurrent.locks.Lock;importjava.util.concurrent.locks.ReentrantLock;/*** 一个线程加一运算,一个线程做减法运算,多个线程同时交替运行
*@authorDenny
**/publicclassCount {privateintnum = 0;privatebooleanflag=false;//标识Lock lock =newReentrantLock();//锁Condition add = lock.newCondition();//加法锁Condition sub = lock.newCondition();//减法锁publicvoidadd() {
lock.lock();//锁上try{while(flag) {//循环判断add.await();
}this.num++;
System.out.println(Thread.currentThread().getName()+ "........" +this.num);this.flag =true;//设置标识sub.signal();//唤醒指定线程}catch(InterruptedException e) {
e.printStackTrace();
}finally{
lock.unlock();
}
}publicvoidsub() {
lock.lock();//锁上try{while(!flag) {//循环判断sub.await();
}this.num--;
System.out.println(Thread.currentThread().getName()+ "........" +this.num);this.flag =false;//设置标识add.signal();//唤醒指定线程}catch(InterruptedException e) {
e.printStackTrace();
}finally{
lock.unlock();
}
}
}
packagecom.pb.thread.demo4;publicclassAddimplementsRunnable {privateCount count;publicAdd(Count count){this.count=count;
}
@Overridepublicvoidrun() {while(true){
count.add();
}
}
}
packagecom.pb.thread.demo4;publicclassSubimplementsRunnable {privateCount count;publicSub(Count count){this.count=count;
}
@Overridepublicvoidrun() {while(true){
count.sub();
}
}
}
packagecom.pb.thread.demo4;publicclassCountTest {publicstaticvoidmain(String[] args) {
Count c=newCount();
Add add=newAdd(c);
Sub sub=newSub(c);
Thread t1=newThread(add);
Thread t2=newThread(add);
Thread t3=newThread(sub);
Thread t4=newThread(sub);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
结果:
Thread-1........1
Thread-3........0
Thread-0........1
Thread-2........0
Thread-1........1
Thread-3........0
Thread-0........1
Thread-2........0