标签:
两个线程交替打印
1.使用synchronized,wait,notify关键字实现
package com.hzm.test; /* * 两个线程交替打印 */ public class Communication { public static void main(String[] args) { // TODO Auto-generated method stub final Business business = new Business(); new Thread(new Runnable(){ @Override public void run() { // TODO Auto-generated method stub for(int j = 0 ;j < 50;j++){ business.sub(j); } } }).start(); for(int j = 0 ;j < 50;j++){ business.main(j); } } } class Business{ private boolean bShouldSub = true; public synchronized void sub(int j){ /* * if改为while语句更健壮 */ if(!bShouldSub){ try { this.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } for(int i = 0;i < 10;i++){ System.out.println("子"+"i= "+i+"j= "+j); } bShouldSub = false; this.notify(); } public synchronized void main(int j){ if(bShouldSub){ try { this.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } for(int i = 0;i < 10;i++){ System.out.println("父"+"i= "+i+"j= "+j); } bShouldSub = true; this.notify(); } }
使用condition信号量实现线程之间通信
package com.condition; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class ConditionTest { public static void main(String[] args) { // TODO Auto-generated method stub final Business business = new Business(); new Thread(new Runnable(){ @Override public void run() { for(int j = 0 ;j < 50;j++){ business.sub(j); } } }).start(); for(int j = 0 ;j < 50;j++){ business.main(j); } } } class Business{ Lock lock = new ReentrantLock(); Condition condition = lock.newCondition(); private boolean bShouldSub = true; public void sub(int j){ /* * if改为while语句更健壮 */ lock.lock(); try{ if(!bShouldSub){ try { //this.wait(); condition.await(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } for(int i = 0;i < 10;i++){ System.out.println("子"+"i= "+i+"j= "+j); } bShouldSub = false; // this.notify(); condition.signal(); }catch(Exception e){ e.printStackTrace(); }finally{ lock.unlock(); } } public void main(int j){ lock.lock(); try{ if(bShouldSub){ try { //this.wait(); condition.await(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } for(int i = 0;i < 10;i++){ System.out.println("父"+"i= "+i+"j= "+j); } bShouldSub = true; //this.notify(); condition.signal(); }finally{ lock.unlock(); } } }
三个线程交替打印,使用信号量实现
package com.test; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class ThreeconditionTest { public static void main(String[] args) { // TODO Auto-generated method stub final Business business = new Business(); new Thread(new Runnable(){ @Override public void run() { for(int j = 0 ;j < 50;j++){ business.sub2(j); } } }).start(); new Thread(new Runnable(){ @Override public void run() { for(int j = 0 ;j < 50;j++){ business.sub3(j); } } }).start(); for(int j = 0 ;j < 50;j++){ business.main(j); } } } class Business{ Lock lock = new ReentrantLock(); Condition condition1 = lock.newCondition(); Condition condition2 = lock.newCondition(); Condition condition3 = lock.newCondition(); private int bShouldSub = 1; public void sub2(int j){ /* * if改为while语句更健壮 */ lock.lock(); try{ if(bShouldSub!=2){ try { //this.wait(); condition2.await(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } for(int i = 0;i < 10;i++){ System.out.println("子"+"i= "+i+"j= "+j); } bShouldSub = 3; // this.notify(); condition3.signal(); }catch(Exception e){ e.printStackTrace(); }finally{ lock.unlock(); } } public void sub3(int j){ /* * if改为while语句更健壮 */ lock.lock(); try{ if(bShouldSub !=3){ try { //this.wait(); condition3.await(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } for(int i = 0;i < 10;i++){ System.out.println("子子"+"i= "+i+"j= "+j); } bShouldSub = 1; // this.notify(); condition1.signal(); }catch(Exception e){ e.printStackTrace(); }finally{ lock.unlock(); } } public void main(int j){ lock.lock(); try{ if(bShouldSub != 1){ try { //this.wait(); condition1.await(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } for(int i = 0;i < 10;i++){ System.out.println("父"+"i= "+i+"j= "+j); } bShouldSub = 2; //this.notify(); condition2.signal(); }finally{ lock.unlock(); } } }
标签:
原文地址:http://www.cnblogs.com/hzmbbbb/p/4280277.html