2.1 Thread Communication -- Pipe
来源:互联网 发布:redis php 编辑:程序博客网 时间:2024/06/08 05:04
Android中的线程间通信一般使用Handler-Looper机制,而我们也可以使用Java中的方式来实现线程间通信。
Pipe(管道)
一个管道为两个线程在同一个进程中提供了一条通道,然后连接并建立一个单方向的通道。在这个通道上,一个生产者线程写数据到管道中,另一个消费者线程从管道中读数据。
在Linux中,Pipe operator是跨进程工作的,而在Java中虚拟机中跨线程工作的(在同一个进程中)。
Pipe简介
工作流程
- 在内存中分配一块环形缓冲器;
- 只有两个连接上的线程可以访问缓冲器,其他线程无法访问到数据,因此确保了线程安全;
- 管道是单方向的,确保了一个写,另外一个读。
传输的数据类型
管道可以传输字节数据(binary-二进制数据)和字符数据(character)。
字节数据使用的类:
PipedOutputStream - 生产者
PipedInputStream - 消费者
字符数据使用的类:
PipedWriter - 生成者
PipedReader - 消费者
声明周期
无论是写线程还是读线程建立了连接,那么就是管道生命周期开始的时候;当这个连接关闭的时候,就是管道生命周期结束的时候。
Pipe的基本使用
管道的生命周期可以分为3步:建立连接,传输数据,断开连接。
1. 建立连接
PipedReader r = new PipedReader();PipedWriter w = new PipedWriter();w.connect(r);
默认的缓存大小是1024,我们可以在通道的消费者端设置这个缓存大小。
int BUFFER_SIZE_IN_CHARS = 1024 * 4;PipedReader r = new PipedReader(BUFFER_SIZE_IN_CHARS);PipedWriter w = new PipedWriter(r);
2.把Reader放入处理线程中
把reader传到一个处理的线程中,然后reader线程开启后,就开始从writer那儿接收数据。
Thread t = new MyReaderThread(r);t.start();
3. 传输数据
// 生成者线程:写入数据w.write('ABC');// 消费者线程:读取数据int result = r.read();
因为生成者可能会写满或者消费者可能读取不到数据,那么就会阻塞。下面的写法更好。
// 生产者线程:写入数据后,flush管道w.writer('ABC');w.flush();// 消费者线程:在一个判断循环中读取数据int i;while((i = reader.read()) != -1) { char c = (char) i; // TODO}
调用flush()方法会通知消费者线程有新的数据可以读取,这样可以减少消费者线程的等待时间。
4.关闭连接
当数据传输完后要关闭通道。
// 生成者线程:关闭writerw.close();// 消费者线程:关闭readerr.close();
如果writer和reader已经建立连接,那么关闭其中一个也是足够的。如果writer关闭,管道就会断开连接,但是缓存的数据仍然可以被读取到;如果reader关闭,那么缓存区会被清空。因此writer和reader要都关闭。
Demo
public class PipeActivity extends Activity { private static final String TAG = PipeActivity.class.getSimpleName(); private EditText pipeEdit = null; private PipedWriter writer = null; private PipedReader reader = null; private Thread readerThread = null; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_pipe); writer = new PipedWriter(); reader = new PipedReader(); try { writer.connect(reader); } catch (IOException e) { e.printStackTrace(); } pipeEdit = (EditText) findViewById(R.id.pipe_edit); pipeEdit.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int start, int before, int count) { try { if (count > before) { writer.write(charSequence.subSequence(before, count).toString()); writer.flush(); } } catch (IOException e) { e.printStackTrace(); } } @Override public void afterTextChanged(Editable editable) { } }); readerThread = new Thread(new TextHandlerRunable(reader)); readerThread.start(); } @Override protected void onDestroy() { super.onDestroy(); readerThread.interrupt(); try { writer.close(); reader.close(); } catch (IOException e) { e.printStackTrace(); } } private static class TextHandlerRunable implements Runnable { private final PipedReader reader; public TextHandlerRunable(PipedReader reader) { this.reader = reader; } @Override public void run() { while (!Thread.currentThread().isInterrupted()) { try { int i; while ((i = reader.read()) != -1) { char c = (char) i; Log.i(TAG, "read: " + c); } } catch (Exception e) { e.printStackTrace(); } } } }}
- 2.1 Thread Communication -- Pipe
- Interprocess Communication——Using Windows Pipe
- process communication by using pipe and tcp
- Linux thread communication and synchronization
- Internet Programming(2)--Single Thread Socket Communication
- 2.2 Thread Communication -- Shared Memory,BlockingQueue
- 2.3 Thread Communication -- Android Message Passing
- Java Thread Programming 1.8.1 - Inter-thread Communication
- Java Thread Programming 1.8.2 - Inter-thread Communication
- Java Thread Programming 1.8.3 - Inter-thread Communication
- Java Thread Programming 1.8.4 - Inter-thread Communication
- Communication
- Communication
- Communication
- System Verilog视频学习笔记(10)- Inter-Thread Communication
- 《Effieicntt Android Threading》 Chapter4 -- Thread Communication(1)
- 《Effieicntt Android Threading》 Chapter4 -- Thread Communication(2)
- 学习笔记-进程间通信(Interprocess communication,IPC) --概念、 pipe and FIFOs
- Android文件上传
- BZOJ2733 永无乡 [启发式合并]
- 浅析NVR主流芯片方案
- 可视化工具调研
- [Leetcode] 279. Perfect Squares 解题报告
- 2.1 Thread Communication -- Pipe
- Symbol
- cookie小例子
- 用python实现一个神经网络
- PreparedStatement ResultSet
- 西门子PLC 使用SCL实现伪随机数
- JAVA安卓开发调用相机
- 九、JVM垃圾算法(GC算法)和垃圾回收器
- 几何模板判断线段与直线相交模板