线程类Thread的API接口分析系列之管道通讯Piped
来源:互联网 发布:100内素数之和 java 编辑:程序博客网 时间:2024/05/16 19:08
Java对于多线程之间的通信提供了一个机制,管道,虽然说线程之间完全可以通过共享变量进行通信,但是java提供了这么一个机制也是应该了解下的,毕竟专门提供的机制肯定有它的好处。由于相对简单(估计内部其实也是用了线程之间的共享变量之类的)就没必要分析什么了,直接上例子
对于字节流:PipedInputStream和PipedOutputStream
public class ThreadApiTest implementsRunnable {
privateString type;
privateObject obj;
ThreadApiTest(Objectobj){
this.obj=obj;
}
/**
*多线程相关api测试
*@author : zhengrf1
* @throws InterruptedException
*@date 创建时间:2017年3月7日下午4:43:15
*/
publicstatic void main(String[] args) throws InterruptedException {
//TODO Auto-generated method stub
PipedInputStreamin = new PipedInputStream();
PipedOutputStream out = newPipedOutputStream();
ThreadApiTestinThread = new ThreadApiTest(in);
ThreadApiTestoutThread = new ThreadApiTest(out);
inThread.type="read";
outThread.type="write";
Threadintest = new Thread(inThread);
Threadouttest = new Thread(outThread);
try{
in.connect(out);
}catch (IOException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
outtest.start();
Thread.sleep(1000);
intest.start();
}
/*@author : zhengrf1
* @date 创建时间:2017年3月7日 下午4:46:48
* @see java.lang.Runnable#run()
*/
@Override
publicvoid run() {
//TODO Auto-generated method stub
if(type.equals("write")){
write();
}
else{
read();
}
}
publicvoid write(){
PipedOutputStreamin = (PipedOutputStream)obj;
try{
out.write("hello".getBytes());
}catch (IOException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
publicvoid read(){
PipedInputStreamin = (PipedInputStream)obj;
try{
byte[]b = new byte[50];
System.out.println(in.read(b));
Strings = new String(b,"utf-8");
System.out.println(s);
}catch (IOException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
}
对于字符流:PipedReader和PipedWriter
public classThreadApiTest implements Runnable {
private Stringtype;
private Objectobj;
ThreadApiTest(Objectobj){
this.obj=obj;
}
/**
*多线程相关api测试
*@author : zhengrf1
* @throws InterruptedException
*@date创建时间:2017年3月7日下午4:43:15
*/
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
// PipedInputStreamin = new PipedInputStream();
// PipedOutputStreamout = new PipedOutputStream();
PipedReaderin = new PipedReader();
PipedWriterout= newPipedWriter();
ThreadApiTestinThread=newThreadApiTest(in);
ThreadApiTestoutThread=newThreadApiTest(out);
inThread.type="read";
outThread.type="write";
Threadintest= newThread(inThread);
Threadouttest= newThread(outThread);
try {
in.connect(out);
}catch(IOExceptione) {
// TODO Auto-generated catch block
e.printStackTrace();
}
outtest.start();
Thread.sleep(1000);
intest.start();
}
/* @author : zhengrf1
* @date 创建时间:2017年3月7日下午4:46:48
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
// TODO Auto-generated method stub
// while(true){
// System.out.println(Thread.currentThread().getName()+"isalive!");
// System.out.println(this.hashCode());
// synchronized(this){
// Thread.currentThread().isAlive();
//// Thread.sleep(100000);
// }
// }
if(type.equals("write")){
write();
}
else{
read();
}
}
public void write(){
PipedWriterout= (PipedWriter)obj;
try {
out.write("hello");
}catch(IOExceptione) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void read(){
PipedReaderin = (PipedReader)obj;
try {
// byte[]b = new byte[50];
char[]b = newchar[50];
System.out.println(in.read(b));
Strings = new String(b);
System.out.println(s);
}catch(IOExceptione) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
--值得注意的是,在java中父类转子类的形式是
PipedWriter out = (PipedWriter)obj;
然后再用out.write去操作
而不是像C++那样强制转换就能使用(PipedWriter)obj.write
另外一个知识点是byte[]转String的方法不能直接
byte[].toString();//这个实际只是上帝类的toString,里面是类名加哈希值
而是使用String的构造方法创建出String对象String s = newString(b,"utf-8");
- 线程类Thread的API接口分析系列之管道通讯Piped
- 线程类Thread的API接口分析系列之interrupt()
- 线程类Thread的API接口分析系列之Condition
- 线程类Thread的API接口分析系列之守护线程setDaemon
- 线程类Thread的API接口分析系列之线程组ThreadGroup
- 线程类Thread的API接口分析系列之读写锁ReentrantReadeWriteLock
- 线程类Thread的API接口分析系列之定时器Timer
- Piped 管道流的使用
- Piped 管道流
- 【Java】线程管道通讯
- Piped*putStream源码分析
- thread API:实现线程类
- 线程(Thread类,Runnable接口)
- 进程间的通讯之有名管道
- 进程之间的通讯之管道
- C#Thread类多线程系列之(三)线程同步技术
- 【Java.IO】I/O 【字节/字符】【节点流】 - 之 - 【管道流】 - Piped*
- 线程的随机性之实现Thread类
- 一剪梅·红藕香残玉簟秋
- Palette: Distributing Tables in Software-Defined Networks笔记
- RabbitMQ官网教程4——路由
- librdkafka的使用和介绍
- JavaScript中的数值转换函数
- 线程类Thread的API接口分析系列之管道通讯Piped
- JAVA设计的六大原则-读书笔记
- maven详解之坐标与依赖
- 《麦肯锡卓越工作法》读书笔记与感谢
- objective-c 成员变量与属性
- 常用的正则表达式,拿走不客气
- [面试] [java] super 关键字的使用
- PTA 数列求和-加强版
- 从Python的0.1输出0.1000000000000001说浮点数的二进制