Java 多线程 通信 通道 (猫狗赛跑)

package thread;

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

public class CommunicateWhitPiping
{
public static void main(String[] args) {
// 创建管道输出流
PipedOutputStream pipedOutputStream = new PipedOutputStream();
// 创建管道输入流
PipedInputStream pipedInputStream = new PipedInputStream();
try {
// 将管道输入流与输出流连接 此过程也可通过重载的构造函数来实现
pipedOutputStream.connect(pipedInputStream);
} catch (IOException e) {
e.printStackTrace();
}
// 创建生产者线程
Producer p = new Producer(pipedOutputStream);
// 创建消费者线程
Consumer c = new Consumer(pipedInputStream);
// 启动线程
p.start();
c.start();
}
}

/**
* 生产者线程(与一个管道输出流相关联)
*/
class Producer extends Thread //狗
{
int x=0;

private PipedOutputStream pos;

public Producer(PipedOutputStream pos) {
this.pos = pos;
}

public void run()
{
while(true)
{

try {
Thread.sleep(500);
} catch (InterruptedException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}

try {

pos.write(this.x);
this.x=this.x+2;
} catch (IOException e)
{
e.printStackTrace();
}
}
}
}

/**
* 消费者线程(与一个管道输入流相关联)
*/
class Consumer extends Thread //猫
{int x=50;
private PipedInputStream pis;

public Consumer(PipedInputStream pis) {
this.pis = pis;
}

public void run()
{
int dogx = 0;
while(true)
{

try {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}

dogx=pis.read();
System.out.println("猫的位置"+this.x);

System.out.println("—————————————狗的位置"+dogx);

this.x++;
if(dogx-this.x>=1)
{
System.out.println("狗超过了猫");
}

} catch (IOException e)
{
e.printStackTrace();
}
}

}
}

时间: 2024-08-10 19:15:56

Java 多线程 通信 通道 (猫狗赛跑)的相关文章

java基础知识回顾之java Thread类学习(八)--java多线程通信等待唤醒机制经典应用(生产者消费者)

 *java多线程--等待唤醒机制:经典的体现"生产者和消费者模型 *对于此模型,应该明确以下几点: *1.生产者仅仅在仓库未满的时候生产,仓库满了则停止生产. *2.消费者仅仅在有产品的时候才能消费,仓空则等待. *3.当消费者发现仓储没有产品可消费的时候,会唤醒等待生产者生产. *4.生产者在生产出可以消费的产品的时候,应该通知等待的消费者去消费. 下面先介绍个简单的生产者消费者例子:本例只适用于两个线程,一个线程生产,一个线程负责消费. 生产一个资源,就得消费一个资源. 代码如下: pub

java基础知识回顾之java Thread类学习(七)--java多线程通信等待唤醒机制(wait和notify,notifyAll)

1.wait和notify,notifyAll: wait和notify,notifyAll是Object类方法,因为等待和唤醒必须是同一个锁,不可以对不同锁中的线程进行唤醒,而锁可以是任意对象,所以可以被任意对象调用的方法,定义在Object基类中. wait()方法:对此对象调用wait方法导致本线程放弃对象锁,让线程处于冻结状态,进入等待线程的线程池当中.wait是指已经进入同步锁的线程,让自己暂时让出同步锁,以便使其他正在等待此锁的线程可以进入同步锁并运行,只有其它线程调用notify方

java多线程通信

最近在研究java多线程,这篇文章主要是介绍一些线程之间的通信: 1:join 的方式,一个线程等待另一个线程执行完毕后在执行,可以控制线程执行的顺序: 场景:B线程要在A线程完成后才开始任务: 不做任何控制的情况下的线程代码如下: @Test public void threadTest4() throws InterruptedException, ExecutionException { // 线程A final Thread threadA = new Thread(new Runnab

【Java多线程通信】syncrhoized下wait()/notify()与ReentrantLock下condition的用法比较

转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6556925.html  一:syncrhoized使用同一把锁的多个线程用通信实现执行顺序的调度 我们知道,使用syncrhoized关键字修饰一个代码块.一个方式时,在代码块.方法执行完毕之前是不会释放掉所持有的锁的,在执行期间,其他申请这个锁的线程只能阻塞等待.这些等待的线程被安排在一个等待队列中. 现在,由于某种原因,在当前同步代码块A中需要另一个同步代码块B先执行完,然后A才能继续进行下去.而同步代

java 多线程通信之管道流

/*   管道流: PipedInputStream void connect(PipedOutputStream src)  使此管道输入流连接到管道输出流 src PipedOutputStream void connect(PipedInputStream snk)   在JDK我们看到PipedInputStream中有管道缓冲区,用来接收数据 管道流内部在实现时还有大量的对同步数据的处理 管道输出流和管道输入流执行时不能互相阻塞,所以一般要开启独立线程分别执行 顺便复习了多线程操作 [

Java多线程通信之两个线程分别打印AB各10次

一道经典的面试题目:两个线程,分别打印AB,其中线程A打印A,线程B打印B,各打印10次,使之出现ABABABABA.. 的效果 1 package com.shangshe.path; 2 3 public class ThreadAB { 4 5 /** 6 * @param args 7 */ 8 public static void main(String[] args) { 9 10 final Print business = new Print(); 11 12 new Threa

java多线程通信之共享变量

(1)当访问共同的代码的时候:可以使用同一个Runnable对象,这个Runnable对象中有这个共享数据,比如卖票系统就可以这么做.或者这个共享数据封装在一个对象当中,然后对这个对象加锁,也可以实现数据安全访问. public class Interfacaesharethread {     public static void main(String[] args) {         Mythread1 mythread = new Mythread1();         new Th

java多线程通信 例子

1 package com.cl.www.thread; 2 3 public class NumberHolder { 4 5 private Integer number = 0; 6 7 // 增加number 8 public synchronized void increaseNum(){ 9 //不是0就不加 10 while(number != 0){ 11 try { 12 this.wait(); 13 } catch (InterruptedException e) { 14

Java多线程通信之wait()和notify()方法

1.wait()方法和sleep()方法: wait()方法在等待中释放锁:sleep()在等待的时候不会释放锁,抱着锁睡眠. 2.notify(): 随机唤醒一个线程,将等待队列中的一个等待线程从等待队列中移到同步队列中. public class Demo_Print { public static void main(String[] args) { Print p = new Print(); new Thread() { public void run() { while (true)