package com.test.socket; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.channels.Pipe; import java.nio.channels.Pipe.SinkChannel; import java.nio.channels.Pipe.SourceChannel; import java.nio.charset.Charset; public class PipeTestMain { private static final byte end = '\n'; /** * @param args */ public static void main(String[] args) { try{ Pipe pipe = Pipe.open(); Thread c1 = new Consumer(pipe.source()); c1.start(); Thread c2 = new Consumer(pipe.source()); c2.start(); Thread c3 = new Consumer(pipe.source()); c3.start(); Thread productor = new Productor(pipe.sink()); productor.start(); Thread.sleep(2000); }catch(Exception e){ e.printStackTrace(); } } static class Consumer extends Thread{ static final Object lock = new Object();//Read a packet SourceChannel channel; Consumer(SourceChannel channel){ this.channel = channel; } @Override public void run(){ try{ Charset charset = Charset.defaultCharset(); ByteBuffer byteBuffer = ByteBuffer.allocate(1024); while(channel.isOpen()){ synchronized (lock) { while(channel.read(byteBuffer) != -1){ int current = byteBuffer.position() - 1; byte last = byteBuffer.get(current); if(last == end){ byteBuffer.position(current);//remove the '\n' break; } } } byteBuffer.flip(); CharBuffer charBuffer = charset.decode(byteBuffer); System.out.println(Thread.currentThread().getId() + ",I get something like this:'" + charBuffer.toString() + "'"); byteBuffer.clear(); } }catch(Exception e){ e.printStackTrace(); }finally{ try{ channel.close(); }catch(Exception ex){ ex.printStackTrace(); } } } } static class Productor extends Thread{ static final Object lock = new Object();//Read a packet SinkChannel channel; Productor(SinkChannel channel){ this.channel = channel; } @Override public void run(){ try{ Charset charset = Charset.defaultCharset(); while(channel.isOpen()){ ///only for write String data = "System-time:" + System.currentTimeMillis(); ByteBuffer byteBuffer = charset.encode(data); appendEnd(byteBuffer); int i = 0; synchronized (lock) { while(byteBuffer.hasRemaining()){ int count = channel.write(byteBuffer); if(count == 0) i++; if(i > 100){ throw new RuntimeException("Pipe has been blocked so long,please check!"); } } } Thread.sleep(1000); } }catch(Exception e){ e.printStackTrace(); }finally{ try{ channel.close(); }catch(Exception ex){ ex.printStackTrace(); } } } } /** * happen-before: buffer must be flip() * @param byteBuffer * @return */ static ByteBuffer appendEnd(ByteBuffer byteBuffer){ int limit = byteBuffer.limit(); if(limit == 0 ){ throw new RuntimeException("Array overflow error.please keep right position."); } byteBuffer.clear(); byteBuffer.position(limit); byteBuffer.put(end); byteBuffer.flip(); return byteBuffer; } }
相关推荐
赠送jar包:httpcore-nio-4.4.6.jar 赠送原API文档:httpcore-nio-4.4.6-javadoc.jar 赠送源代码:httpcore-nio-4.4.6-sources.jar 包含翻译后的API文档:httpcore-nio-4.4.6-javadoc-API文档-中文(简体)版.zip ...
赠送jar包:httpcore-nio-4.4.15.jar 赠送原API文档:httpcore-nio-4.4.15-javadoc.jar 赠送源代码:httpcore-nio-4.4.15-sources.jar 包含翻译后的API文档:httpcore-nio-4.4.15-javadoc-API文档-中文(简体)版....
赠送jar包:httpcore-nio-4.4.10.jar; 赠送原API文档:httpcore-nio-4.4.10-javadoc.jar; 赠送源代码:httpcore-nio-4.4.10-sources.jar; 赠送Maven依赖信息文件:httpcore-nio-4.4.10.pom; 包含翻译后的API文档...
赠送jar包:xnio-nio-3.8.0.Final.jar; 赠送原API文档:xnio-nio-3.8.0.Final-javadoc.jar; 赠送源代码:xnio-nio-3.8.0.Final-sources.jar; 赠送Maven依赖信息文件:xnio-nio-3.8.0.Final.pom; 包含翻译后的API...
赠送jar包:httpcore-nio-4.4.10.jar; 赠送原API文档:httpcore-nio-4.4.10-javadoc.jar; 赠送源代码:httpcore-nio-4.4.10-sources.jar; 赠送Maven依赖信息文件:httpcore-nio-4.4.10.pom; 包含翻译后的API文档...
赠送jar包:httpcore-nio-4.4.15.jar 赠送原API文档:httpcore-nio-4.4.15-javadoc.jar 赠送源代码:httpcore-nio-4.4.15-sources.jar 包含翻译后的API文档:httpcore-nio-4.4.15-javadoc-API文档-中文(简体)-...
赠送jar包:xnio-nio-3.8.4.Final.jar; 赠送原API文档:xnio-nio-3.8.4.Final-javadoc.jar; 赠送源代码:xnio-nio-3.8.4.Final-sources.jar; 赠送Maven依赖信息文件:xnio-nio-3.8.4.Final.pom; 包含翻译后的API...
赠送jar包:xnio-nio-3.8.0.Final.jar; 赠送原API文档:xnio-nio-3.8.0.Final-javadoc.jar; 赠送源代码:xnio-nio-3.8.0.Final-sources.jar; 赠送Maven依赖信息文件:xnio-nio-3.8.0.Final.pom; 包含翻译后的API...
赠送jar包:httpcore-nio-4.4.5.jar; 赠送原API文档:httpcore-nio-4.4.5-javadoc.jar; 赠送源代码:httpcore-nio-4.4.5-sources.jar; 赠送Maven依赖信息文件:httpcore-nio-4.4.5.pom; 包含翻译后的API文档:...
java运行依赖jar包
赠送jar包:httpcore-nio-4.4.14.jar; 赠送原API文档:httpcore-nio-4.4.14-javadoc.jar; 赠送源代码:httpcore-nio-4.4.14-sources.jar; 赠送Maven依赖信息文件:httpcore-nio-4.4.14.pom; 包含翻译后的API文档...
赠送jar包:httpcore-nio-4.4.6.jar; 赠送原API文档:httpcore-nio-4.4.6-javadoc.jar; 赠送源代码:httpcore-nio-4.4.6-sources.jar; 赠送Maven依赖信息文件:httpcore-nio-4.4.6.pom; 包含翻译后的API文档:...
赠送jar包:httpcore-nio-4.4.12.jar; 赠送原API文档:httpcore-nio-4.4.12-javadoc.jar; 赠送源代码:httpcore-nio-4.4.12-sources.jar; 赠送Maven依赖信息文件:httpcore-nio-4.4.12.pom; 包含翻译后的API文档...
赠送jar包:xnio-nio-3.8.4.Final.jar; 赠送原API文档:xnio-nio-3.8.4.Final-javadoc.jar; 赠送源代码:xnio-nio-3.8.4.Final-sources.jar; 赠送Maven依赖信息文件:xnio-nio-3.8.4.Final.pom; 包含翻译后的API...
《深入解析httpcore-nio-4.3.jar:构建高性能的Java非阻塞网络通信》 在Java网络编程中,高效、稳定且可扩展的通信框架至关重要。Apache HttpComponents项目中的HttpCore NIO模块(httpcore-nio)就是这样一个框架...
赠送jar包:httpcore-nio-4.4.4.jar; 赠送原API文档:httpcore-nio-4.4.4-javadoc.jar; 赠送源代码:httpcore-nio-4.4.4-sources.jar; 赠送Maven依赖信息文件:httpcore-nio-4.4.4.pom; 包含翻译后的API文档:...
赠送jar包:httpcore-nio-4.4.12.jar; 赠送原API文档:httpcore-nio-4.4.12-javadoc.jar; 赠送源代码:httpcore-nio-4.4.12-sources.jar; 赠送Maven依赖信息文件:httpcore-nio-4.4.12.pom; 包含翻译后的API文档...
赠送jar包:httpcore-nio-4.4.4.jar; 赠送原API文档:httpcore-nio-4.4.4-javadoc.jar; 赠送源代码:httpcore-nio-4.4.4-sources.jar; 赠送Maven依赖信息文件:httpcore-nio-4.4.4.pom; 包含翻译后的API文档:...
赠送jar包:httpcore-nio-4.4.5.jar; 赠送原API文档:httpcore-nio-4.4.5-javadoc.jar; 赠送源代码:httpcore-nio-4.4.5-sources.jar; 赠送Maven依赖信息文件:httpcore-nio-4.4.5.pom; 包含翻译后的API文档:...
java运行依赖jar包