结果:

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

使用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

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航