学习多线程3---线程之间的通信
2015-02-08 18:09
232 查看
两个线程交替打印
1.使用synchronized,wait,notify关键字实现
使用condition信号量实现线程之间通信
三个线程交替打印,使用信号量实现
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(); } } }
相关文章推荐
- 学习java多线程的笔记3-使用BlockingQueue阻塞队列来模拟两个线程之间的通信
- 基础学习day12--多线程一线程之间的通信和常用方法
- JAVA基础学习(十二)--多线程一线程之间的通信
- 线程学习笔记【4】---线程之间通信
- vc 基于对话框多线程编程实例——线程之间的通信
- 多线程学习07-线程间通信
- java多线程---线程之间的通信
- 黑马程序员_多线程的线程间通信学习笔记
- 多线程学习笔记四--------------线程间通信问题
- vc 基于对话框多线程编程实例——线程之间的通信
- Java多线程(3) 线程之间通信
- vc 基于对话框多线程编程实例——线程之间的通信
- .net学习之多线程、线程死锁、线程通信 生产者消费者模式、委托的简单使用、GDI(图形设计接口)常用的方法
- Java基础学习__多线程(线程间通信--生产者消费者JDK5.0升级版)
- Condidtion实现多线程之间的线程通信
- Java 多线程编程之六:线程之间的通信(附源代码)
- java学习——如何实现线程之间的通信 ,Condition 的使用
- Java基础学习6_多线程(线程间通信--生产者消费者)
- 多线程-线程之间的通信
- iOS多线程学习---控制线程通信及线程使用的总结