多线程之间的通信~~~管道通道

简介: 多线程之间的通信~~~管道通道

多线程之间的通信~~~管道通道

管道流:主要用于线程之间的通信传输的媒介是内存!传输的媒介是内存,传输的媒介是内存,传输的媒介是内存

其实就跟咱之前学得io 流操作一致,只是在io 流 基础上结合了线程(任务)的知识!

(ps: 线程之间的通信模式:生产者消费者模式通过“信息通道”------内存缓存区)

(1)字节流、字符流的管道通道

字节流:PipedInputStream,PipedOutputStream

字符流:PipedRead、PipedWriter

(2) 将输入流和输出流进行连接,用connect() 方法,方便在多个线程中传输数据。

writer.connect(reader);     //等价于 reader.connect(writer);  因为你连我,就是我连你呀

(3) 连接后,两个线程就可以进行通信了。相当于通过写入通道写入的资源 流通到了(connect)  读取通道里。

       //发送消息、接收消息

       new Thread(new SendMessegeTask(pipOutStream)).start();

       new Thread(new AcceptMessegeTask(pipedInStream)).start();

 

案例代码:

 发送消息 class SendMessegeTask


package ChatWithSelf;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.Scanner;
/**
 * 发送消息
 * @author Huangyujun
 *
 */
public class SendMessegeTask implements Runnable {
    private PipedOutputStream pipOutStream;
    public SendMessegeTask(PipedOutputStream pipOutStream) {
        this.pipOutStream = pipOutStream;
    }
    Scanner scanner = new Scanner(System.in);
    @Override
    public void run() {
        while(true) {
            // 不断地写入
            System.out.println("请输入要发生的内容:");
            String content = scanner.next();
            byte[] byteContent = content.getBytes();
//            new byte[1024];
//            byteContent = content.getBytes();
            try {
                pipOutStream.write(byteContent, 0, byteContent.length);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}


 接收消息 class AcceptMessegeTask


package ChatWithSelf;
import java.io.IOException;
import java.io.PipedInputStream;
/**
 * 接收消息
 * @author Huangyujun
 *
 */
public class AcceptMessegeTask implements Runnable{
    private PipedInputStream pipedInStream;
    public AcceptMessegeTask(PipedInputStream pipedInStream) {
        this.pipedInStream = pipedInStream;
    }
    @Override
    public void run() {
        while(true) {
            // 不断地读取,前面接收的是字节数组的内容
            byte[] buffer = new byte[1024];
            int read = -1;
            try {
                while((read = pipedInStream.read(buffer)) != -1) {
                    //打印到公屏上
                    System.out.println(new String(buffer));
                    //为下一次做空间准备
                    buffer = new byte[1024];
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }    
    }
}


 管道通信 class Piped


package ChatWithSelf;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.Scanner;
/**
 * 管道通信
 * @author Huangyujun
 *
 */
public class Piped {
    public static void main(String[] args) throws IOException {
        PipedInputStream pipedInStream = new PipedInputStream();
        PipedOutputStream pipOutStream = new PipedOutputStream();
        //联通
//        pipOutStream.connect(pipedInStream);
        pipedInStream.connect(pipOutStream);
        //发送消息、接收消息
        new Thread(new SendMessegeTask(pipOutStream)).start(); 
        new Thread(new AcceptMessegeTask(pipedInStream)).start(); 
    }
}



目录
相关文章
|
3月前
|
Java
实现Java多线程中的线程间通信
实现Java多线程中的线程间通信
|
4月前
|
Java 程序员
从菜鸟到大神:JAVA多线程通信的wait()、notify()、notifyAll()之旅
【6月更文挑战第21天】Java多线程核心在于wait(), notify(), notifyAll(),它们用于线程间通信与同步,确保数据一致性。wait()让线程释放锁并等待,notify()唤醒一个等待线程,notifyAll()唤醒所有线程。这些方法在解决生产者-消费者问题等场景中扮演关键角色,是程序员从新手到专家进阶的必经之路。通过学习和实践,每个程序员都能在多线程编程的挑战中成长。
44 6
|
4月前
|
Java
Java Socket编程与多线程:提升客户端-服务器通信的并发性能
【6月更文挑战第21天】Java网络编程中,Socket结合多线程提升并发性能,服务器对每个客户端连接启动新线程处理,如示例所示,实现每个客户端的独立操作。多线程利用多核处理器能力,避免串行等待,提升响应速度。防止死锁需减少共享资源,统一锁定顺序,使用超时和重试策略。使用synchronized、ReentrantLock等维持数据一致性。多线程带来性能提升的同时,也伴随复杂性和挑战。
81 0
|
4月前
|
安全 Java
JAVA多线程通信新解:wait()、notify()、notifyAll()的实用技巧
【6月更文挑战第20天】Java多线程中,`wait()`, `notify()`和`notifyAll()`用于线程通信。在生产者-消费者模型示例中,它们确保线程同步。`synchronized`保证安全,`wait()`在循环内防止虚假唤醒,`notifyAll()`避免唤醒单一线程问题。关键技巧包括:循环内调用`wait()`,优先使用`notifyAll()`以保证可靠性,以及确保线程安全和正确处理`InterruptedException`。
40 0
|
2月前
|
存储 安全 Java
【多线程面试题 七】、 说一说Java多线程之间的通信方式
Java多线程之间的通信方式主要有:使用Object类的wait()、notify()、notifyAll()方法进行线程间协调;使用Lock接口的Condition的await()、signal()、signalAll()方法实现更灵活的线程间协作;以及使用BlockingQueue作为线程安全的队列来实现生产者和消费者模型的线程通信。
|
4月前
|
Java 开发者
线程通信的方法和实现技巧详解
线程通信的方法和实现技巧详解
|
3月前
|
Java
实现Java多线程中的线程间通信
实现Java多线程中的线程间通信
|
3月前
|
消息中间件 安全 Java
Java中的线程间通信详解
Java中的线程间通信详解
|
3月前
|
消息中间件 Python
线程通信
【7月更文挑战第1天】
28 2
|
3月前
|
Java
线程间通信的几种方法
线程间通信的几种方法