- 浏览: 524553 次
- 性别:
- 来自: 杭州
文章分类
最新评论
-
飞天奔月:
public List<String> gener ...
实践中的重构30_不做油漆匠 -
在世界的中心呼喚愛:
在世界的中心呼喚愛 写道public class A {
...
深入理解ReferenceQueue GC finalize Reference -
在世界的中心呼喚愛:
在世界的中心呼喚愛 写道在世界的中心呼喚愛 写道在classB ...
深入理解ReferenceQueue GC finalize Reference -
在世界的中心呼喚愛:
在世界的中心呼喚愛 写道在classB的finalize上打断 ...
深入理解ReferenceQueue GC finalize Reference -
在世界的中心呼喚愛:
iteye比较少上,如果可以的话,可以发e-mail交流:ch ...
深入理解ReferenceQueue GC finalize Reference
今天想做一个System.out的重定向,想要得的结果是有很多个线程写System.out,
利用PipedStread重定向一个JPanel,结果拿到一堆IOException("Write end dead").
看看PipedStream的code,发现里面有些东东值得探讨。
原有的PipedOutputStream和PipedInputStream的工作原理是这样的。
发往PipedOutputStream的byte写到PipedInputStream的一个cache中,基于PipedInputStream
构造一个标准的生产者消费者的锁协议。
查看源代码,有如下问题。
PipedInputStream的connect方法是没有加锁的,PipedOutputStream的connect方法只对PipedOutputStream加锁,
这样当有两个线程同时调用同一个PipedInputStream的connect方法时是有问题的。改啊。
在PipedInputStream里面有Thread readSide和Thread writeSide来分别记录Read thread和write thread.
在每次接收bytes和cache满的时候都要检查readSide,以防止没有reader来读取,这点还是有用的。
但是writeSide的使用导致PipedStream的WriteThread必须在另一个线程读cache的时候为活的。这个也是有一定
意义的,因为pipedStream的doc,已经说明了适用的情况是一个线程写pipe,一个线程读pipe。
但是如果在多个线程写pipe的时候就是一个妨碍了。
看到这里,不是pipedStream不好,是我想要的和pipedStream的spec不吻合。
我想要的是一个可以多个线程写入,单线程读出的Stream对.
利用PipedStread重定向一个JPanel,结果拿到一堆IOException("Write end dead").
看看PipedStream的code,发现里面有些东东值得探讨。
原有的PipedOutputStream和PipedInputStream的工作原理是这样的。
发往PipedOutputStream的byte写到PipedInputStream的一个cache中,基于PipedInputStream
构造一个标准的生产者消费者的锁协议。
查看源代码,有如下问题。
public void connect(PipedOutputStream src) throws IOException { src.connect(this); } public synchronized void connect(PipedInputStream snk) throws IOException { if (snk == null) { throw new NullPointerException(); } else if (sink != null || snk.connected) { throw new IOException("Already connected"); } sink = snk; snk.in = -1; snk.out = 0; snk.connected = true; }
PipedInputStream的connect方法是没有加锁的,PipedOutputStream的connect方法只对PipedOutputStream加锁,
这样当有两个线程同时调用同一个PipedInputStream的connect方法时是有问题的。改啊。
public synchronized void connect(MyPipedInputStream snk) throws IOException { if (snk == null) { throw new NullPointerException(); } if (sink != null) { throw new IOException("Already connected"); } synchronized (snk) { if (snk.connected) { throw new IOException("Already connected"); } sink = snk; snk.in = -1; snk.out = 0; snk.connected = true; } }
在PipedInputStream里面有Thread readSide和Thread writeSide来分别记录Read thread和write thread.
在每次接收bytes和cache满的时候都要检查readSide,以防止没有reader来读取,这点还是有用的。
但是writeSide的使用导致PipedStream的WriteThread必须在另一个线程读cache的时候为活的。这个也是有一定
意义的,因为pipedStream的doc,已经说明了适用的情况是一个线程写pipe,一个线程读pipe。
但是如果在多个线程写pipe的时候就是一个妨碍了。
看到这里,不是pipedStream不好,是我想要的和pipedStream的spec不吻合。
我想要的是一个可以多个线程写入,单线程读出的Stream对.
/** * 多线程写,单线程读的piped stream对. */ public class MyOutputStream extends OutputStream { private MyInputStream sink; private synchronized void connect(MyInputStream snk) throws IOException { if (snk == null) { throw new NullPointerException(); } if (sink != null) { throw new IOException("Already connected"); } synchronized (snk) { if (snk.isConnected()) { throw new IOException("Already connected"); } snk.connect(); sink = snk; } } public MyOutputStream(MyInputStream snk) throws IOException { connect(snk); } @Override public void write(int b) throws IOException { if (sink == null) { throw new IOException("Pipe not connected"); } sink.receive(b); } @Override public void write(byte b[], int off, int len) throws IOException { if (sink == null) { throw new IOException("Pipe not connected"); } sink.receive(b, off, len); } /** * No need to close this stream, the piped stream pair should closed by read * side. */ @Override public void close() { } } /** * 多线程写,单线程读的piped stream对. */ public class MyInputStream extends InputStream { private static final int BUFFER_SIZE = 1024; private byte buffer[] = new byte[BUFFER_SIZE];; private int in = 0; private int out = 0; private int dataLength = 0; private boolean connected = false; private boolean isClosed = false; public MyInputStream() { } private void checkState() throws IOException { if (!connected) { throw new IOException("Pipe not connected."); } if (isClosed) { throw new IOException("Stream is closed."); } } private int readCore() throws IOException { int ret = buffer[out] & 0xFF; if (out < buffer.length - 1) { out++; } else { out = 0; } dataLength--; return ret; } private void receiveCore(int b) throws IOException { buffer[in] = (byte) (b & 0xFF); if (in < buffer.length - 1) { in++; } else { in = 0; } dataLength++; } synchronized private void waitSpace() throws IOException { while (dataLength == buffer.length) { try { notifyAll(); wait(); } catch (InterruptedException e) { throw new InterruptedIOException(); } } } synchronized private void waitData() throws IOException { while (dataLength == 0) { try { notifyAll(); wait(); } catch (InterruptedException e) { throw new InterruptedIOException(); } } } synchronized boolean isConnected() { return connected; } synchronized void connect() { connected = true; } synchronized void receive(int b) throws IOException { checkState(); waitSpace(); receiveCore(b); notifyAll(); } synchronized void receive(byte b[], int off, int len) throws IOException { if (b == null) { throw new NullPointerException(); } else if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0)) { throw new IndexOutOfBoundsException(); } else if (len == 0) { return; } checkState(); waitSpace(); if (len <= buffer.length - dataLength) { for (int i = off; i < off + len; i++) { receiveCore(b[i]); } notifyAll(); } else { int realTransfer = buffer.length - dataLength; for (int i = off; i < off + realTransfer; i++) { receiveCore(b[i]); } notifyAll(); receive(b, off + realTransfer, len - realTransfer); } } @Override synchronized public int read() throws IOException { checkState(); waitData(); int ret = readCore(); notifyAll(); return ret; } @Override synchronized public int read(byte b[], int off, int len) throws IOException { if (b == null) { throw new NullPointerException(); } else if (off < 0 || len < 0 || len > b.length - off) { throw new IndexOutOfBoundsException(); } else if (len == 0) { return 0; } checkState(); waitData(); int realTransfer = Math.min(dataLength, len); for (int i = off; i < off + realTransfer; i++) { b[i] = (byte) (readCore() & 0xFF); } notifyAll(); return realTransfer; } @Override synchronized public int available() throws IOException { checkState(); return dataLength; } @Override synchronized public void close() { isClosed = true; buffer = null; } /** * n<=Int.Max */ @Override synchronized public long skip(long n) throws IOException { checkState(); if (n <= 0) { return 0; } int realSkip = (int) Math.min(dataLength, n); dataLength = dataLength - realSkip; out = (out + realSkip) % buffer.length; return realSkip; } }
发表评论
-
java对象的大小_基础知识
2014-09-14 16:51 1725引言 Java的对象被jvm管理,单个对象如何布局,大小如何, ... -
xml encoding和实际编码不同导致xml解析异常
2014-04-10 09:52 4972发现一个xml encoding和实际编码不同导致xml解析异 ... -
按照bit读取或写入java的IO流
2012-11-18 22:00 3763写了个按照bit读取或写入java的IO流的简单代码,保留在博 ... -
类构造函数clinit尽量简单化
2012-01-29 16:30 1385java的类构造方法只能执行一次(不考虑多个类加载器和类卸载的 ... -
使用ImageIO.write存储png格式图片性能较差问题
2011-12-27 19:06 29155目前加载一个png格式的图片,做一些绘图工作,发现ImageI ... -
GC iCMS一次调优
2011-12-23 20:00 0原有GC参数: -server -XX:+UseParNew ... -
java集合框架类源代码阅读体会(Java Collections Framework)
2011-09-17 23:55 4995忘了什么原因突然想看下JCF,于是就有了这个阅读体会。 jav ... -
[gc] GC调优及awk脚本分析GC日志
2011-07-20 19:26 2089原有GC参数 JAVA_OPTS="-server ... -
jvm性能查看工具
2011-06-18 11:48 1584jps查看所有java进程。 jconsole jvisu ... -
[gc] java内存管理以及GC
2011-03-27 13:25 4426目录 内存管理简介 GC简介 好的Collector的特性 ... -
object的hash code
2011-01-03 19:40 2089sun的jvm默认的hash code返回的是对象的内部地址构 ... -
Enum简介
2010-08-16 21:51 1485java的Enum不同于c的命名整型常量,它本身是有类型的,而 ... -
java1.5中{@inheritDoc}的使用
2010-07-12 23:14 9568java1.5中@Override还不能用 ... -
[code] 大量只读线程安全的FastHashMap
2010-06-25 17:27 2277org.apache.commons.collections. ... -
[code] 继承TableRowSorter的一个小陷阱
2010-01-09 21:54 1263在一个JTable里面想做sorting。 继承了TableR ... -
深入理解java的finalize
2009-10-11 01:23 19156目录 基本预备相关知 ... -
深入理解java的clone
2009-10-09 14:13 4580目录 预备知识 为什么 ... -
简明OPhone/Android入门体验(有图有源码)
2009-09-25 00:34 3331主要参考 http://code.google.com/p/a ... -
深入理解ReferenceQueue GC finalize Reference
2009-06-22 22:55 20084关于对象如何销毁以及f ...
相关推荐
linux 系统的编程的源码,对初学者很有帮助。多线程、内存、信号量等程序。
N个线程对完成的A表数据做最后处理 支持大数据量跑批,就是个例子,本来是公司发送促销邮件用的,欢迎提意见和建议。 运行DispatcherMain可以测试,库结构自己可以根据code随便改成父子表关系的就行
多线程端口扫描 源码 sourcecode 多线程端口扫描 源码 sourcecode
C#多线程编程实战Code源代码 资源是从华章出版社官网下载的
看《C#线程参考手册》时候发现相关代码url已经“不存在”,自己手打了一遍,调试可运行了。
win32多线程程序 EXITCOD 讲解 如何使用线程
该例程是针对MFC的多线程编程,具体的code介绍见博客地址:http://blog.csdn.net/u013896064 window线程编程
VC++多线程访问数据库的实例,多线程的意思是说程序在执行当前任务的时候,可以同时执行其它程序,源代码是从国外网站而来,www.codefans.net运行如上示。 源码爱好者:编译后请将数据库转移至Debug目录下。
void CDlgMain::ExitPayThread() { if(m_hThread==NULL) return ; DWORD rc=WaitForSingleObject(m_hThread,1000);//等待10秒, 然后开始杀线程 ... AfxMessageBox("强行退出一个工作线程时发生异常"); } } } }
这是一个多线程的示例工程,非常适合新手学习! 这是一个多线程的示例工程,非常适合新手学习! 这是一个多线程的示例工程,非常适合新手学习! 这是一个多线程的示例工程,非常适合新手学习! 这是一个多线程的示例...
此C#多线程编程实战7644OT_Code文件非常好用,绝对是纯干货!
Feedback shift registers Stream ciphers based on LFSRs Other stream ciphers
之前做过基于开发板的单线程2D游戏的开发!根据印象写的一个通用定时器,希望以后用的着,有需要的朋友欢迎下载交流! 例子请看资源:“VC写的字符界面贪吃蛇(原创)- 不闪屏” 环境:code::blocks + MinGW PS:...
pin如何支持多线程,pin为什么串行处理拦截的code,以及如何管理code cache的,intel pin groups 在这篇paper中给了很好的解答。
windows-CODE注入(远程线程注入) 整理代码
多线程网络下载,修改网络地址即可用
delphi stream player DirectShow filter source code
VB api编程模块Stream图像编程代码VB api programming code programming model Stream image
用信号量实现两个线程间的交替执行,相对于条件变量更简单
dotNetFramework多线程编程,多线程编程问题