线程间通信
来源:互联网 发布:pack php java 编辑:程序博客网 时间:2024/05/21 14:02
1.多线程间使用共享变量
3.通过线程A调用线程B方法,对线程B变量做处理
public class CommunicateByShareVaria { public static void main(String[] args) { InnerShareVaria innerShareVaria = new InnerShareVaria(); Thread thread = new Thread(innerShareVaria.getMyThread()); Thread thread1 = new Thread(innerShareVaria.getMyThread()); Thread threa2 = new Thread(innerShareVaria.getMyThread()); thread1.start(); threa2.start(); thread.start(); }}class InnerShareVaria { private int flag; class MyThread implements Runnable { public synchronized void run() {//由于为内部类,类被类加载器加载的时候,就会在内存中保留一份数据,所以要对run()方法加锁,实现数据flag同步 for (int i = 0; i < 50; i++) { System.out.println(Thread.currentThread().getName() + " is running and flag is " + flag++); } } } public Runnable getMyThread() { return new MyThread(); //保证每个线程都不一样 }}
2.通过pipe管道流,将生产者线程与消费者线程连接起来
public class CommunicateByPipe { public static void main(String[] args) { try { PipedInputStream pipedInputStream = new PipedInputStream(); PipedOutputStream pipedOutputStream = new PipedOutputStream(); Producer producer = new Producer(pipedOutputStream); Consumer consumer = new Consumer(pipedInputStream); pipedInputStream.connect(pipedOutputStream);//输出、输入流管道连接,相当于producer与consumer相连 Thread thread = new Thread(producer); Thread thread1 = new Thread(consumer); thread.start(); thread1.start(); } catch (Exception e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } }}class Producer implements Runnable { private PipedOutputStream pipedOutputStream; public Producer(PipedOutputStream pipedOutputStream) { this.pipedOutputStream = pipedOutputStream; } public void run() { try { Thread.sleep(5000); String data = "producer往管道中开始写数据了"; pipedOutputStream.write(data.getBytes("utf-8")); } catch (Exception e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } }}class Consumer implements Runnable { private PipedInputStream pipedInputStream; public Consumer(PipedInputStream pipedInputStream) { this.pipedInputStream = pipedInputStream; } public void run() { System.out.println("consumer is waiting data from data"); try { int byteNum = pipedInputStream.read(); System.out.println("consumer read " + byteNum + "b ytes data from pipe"); } catch (IOException e) { } //To change body of implemented methods use File | Settings | File Templates. }}
3.通过线程A调用线程B方法,对线程B变量做处理
public class CommunicateByCommonInterface { public static void main(String[] args) { ThreadB threadB = new ThreadB(); ThreadA threadA = new ThreadA(threadB); Thread thread = new Thread(threadA); Thread thread2 = new Thread(threadA); Thread thread3 = new Thread(threadA); thread.start(); thread2.start(); thread3.start(); }}class ThreadA implements Runnable { private ThreadB threadB; public ThreadA(ThreadB threadB) { this.threadB = threadB; } public void run() { for (int i = 0; i < 20; ++i) { System.out.println(Thread.currentThread().getName() + " running flag is " ); System.out.println(threadB.getFlag()); } //必须等线程ThreadB方法getFlag()方法返回才能继续其他操作 //To change body of implemented methods use File | Settings | File Templates. }}class ThreadB implements Runnable { public int flag; public void run() { //To change body of implemented methods use File | Settings | File Templates. } public int getFlag() { try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } return flag++; }}
0 0
- 线程同步--线程间通信
- 线程同步--线程间通信
- JAVASE线程---线程间通信
- JAVA线程-线程间通信
- 线程3:线程间通信
- 线程与线程间通信
- 线程间通信
- c#线程间通信
- VC 线程间通信
- 线程间通信 ManualResetEvent
- java 线程间通信
- android线程间通信
- VC 线程间通信
- 线程间通信方式
- QT---线程间通信
- Android线程间通信
- 线程间的通信
- linux线程间通信
- Java GUI编程(五)鼠标事件:实现画点
- 2014版学车考驾照精华攻略 总有一个你需要!赶紧收藏吧!!
- Nyoj 38 布线问题
- Pexpect 的实例分析
- Hadoop从听说到入门
- 线程间通信
- SeekBar的使用(一):实现OnSeekBarChangListener
- iOS 使用 dispatch_once 创建线程安全的单例
- Android 《手机卫士》随听笔记
- LeetCode|Permutations
- <你必须知道的495个C语言问题>学习笔记 (一)
- Python3.2官方文档--标准接口和dir()函数
- 次小生成树
- C/C++/PHP等语言 在线编译器