`
QING____
  • 浏览: 2253286 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

NIO-Pile通道实例

    博客分类:
  • JAVA
 
阅读更多
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;
	}

}

 

分享到:
评论

相关推荐

    httpcore-nio-4.4.6-API文档-中文版.zip

    赠送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 ...

    httpcore-nio-4.4.15-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文档-中文(简体)版....

    httpcore-nio-4.4.10-API文档-中文版.zip

    赠送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文档...

    xnio-nio-3.8.0.Final-API文档-中文版.zip

    赠送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...

    httpcore-nio-4.4.10-API文档-中英对照版.zip

    赠送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文档...

    httpcore-nio-4.4.15-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文档-中文(简体)-...

    xnio-nio-3.8.4.Final-API文档-中英对照版.zip

    赠送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...

    xnio-nio-3.8.0.Final-API文档-中英对照版.zip

    赠送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...

    httpcore-nio-4.4.5-API文档-中文版.zip

    赠送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文档:...

    nio-multipart-parser-1.1.0.jar

    java运行依赖jar包

    httpcore-nio-4.4.14-API文档-中文版.zip

    赠送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文档...

    httpcore-nio-4.4.6-API文档-中英对照版.zip

    赠送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文档:...

    httpcore-nio-4.4.12-API文档-中文版.zip

    赠送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文档...

    xnio-nio-3.8.4.Final-API文档-中文版.zip

    赠送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包

    《深入解析httpcore-nio-4.3.jar:构建高性能的Java非阻塞网络通信》 在Java网络编程中,高效、稳定且可扩展的通信框架至关重要。Apache HttpComponents项目中的HttpCore NIO模块(httpcore-nio)就是这样一个框架...

    httpcore-nio-4.4.4-API文档-中文版.zip

    赠送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文档:...

    httpcore-nio-4.4.12-API文档-中英对照版.zip

    赠送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文档...

    httpcore-nio-4.4.4-API文档-中英对照版.zip

    赠送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文档:...

    httpcore-nio-4.4.5-API文档-中英对照版.zip

    赠送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文档:...

    httpcore-nio-4.4.12.jar

    java运行依赖jar包

Global site tag (gtag.js) - Google Analytics