- 浏览: 98903 次
- 性别:
- 来自: 北京
最新评论
-
wangshengyuan:
非常感谢博主,真是站在了巨人的肩膀上了
(五)用JAVA编写MP3解码器——解析文件信息 -
落枫飘飘:
楼主,你的播放器下载地址http://jmp123.sf.n ...
(附)用JAVA编写MP3解码器——GUI -
lfp001:
解码纯短块的增益因子时,解码得到12个频带的值。在逆量化纯短块 ...
(十)用JAVA编写MP3解码器——逆量化和重排序 -
lfp001:
逆量化混合块时:前8个频带是长块,用长块公式逆量化;后9个频带 ...
(八)用JAVA编写MP3解码器——解码增益因子 -
clearstarrysky:
短块:576个频谱值分为13个增益因子频带,但是在解码增益因子 ...
(十)用JAVA编写MP3解码器——逆量化和重排序
如果用HttpURLConnection类的方法打开连接,然后用InputStream类获得输入流,再用BufferedInputStream构造出带缓冲区的输入流,如果网速太慢的话,无论缓冲区设置多大,听起来都是断断续续的,达不到真正缓冲的目的。于是尝试编写代码实现用缓冲方式读取远程文件,以下贴出的代码是我写的MP3解码器的一部分。我是不怎么赞同使用多线程下载的,加之有的链接下载速度本身就比较快,所以在下载速度足够的情况下,就让下载线程退出,直到只剩下一个下载线程。多线程中的同步、HttpURLConnection的超时阻塞等因素都会使代码看起来异常复杂。
问题模型 数组buf[],N个“写”线程从文件顺序读入固定长度数据写入buf[];一个“读”线程按照文件内容顺序从缓冲区读取,一次读任意长度。一个写线程发生错误或文件读完,这个读线程和所有写线程退出。
针对上述问题,简要介绍一下实现多线程环形缓冲的方法。将缓冲区buf[]分为16块,每块32K,下载线程负责向缓冲区写数据,每次写一块;每次读小于32K的任意字节。线程同步:各个写线程互斥等待空闲块,用信号量机制分配空闲块;各写线程并发填写buf[];读线程和各写线程并发使用buf[]。
为突出主题略去了一些和本文无关的代码。
一、HttpReader类功能:HTTP协议从指定URL读取数据。
package instream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; public final class HttpReader { public static final int MAX_RETRY = 10; private URL url; private HttpURLConnection httpConnection; private InputStream in_stream; private long cur_pos; //决定seek方法中是否执行文件读取定位 private int connect_timeout; private int read_timeout; public HttpReader(URL u) { this(u, 5000, 5000); } public HttpReader(URL u, int connect_timeout, int read_timeout) { this.connect_timeout = connect_timeout; this.read_timeout = read_timeout; url = u; } public int read(byte[] b, int off, int len) throws IOException { int r = in_stream.read(b, off, len); cur_pos += r; return r; } /* * 抛出异常通知重试. * 例如响应码503可能是由某种暂时的原因引起的,同一IP频繁的连接请求会遭服务器拒绝. */ public void seek(long start_pos) throws IOException { if (start_pos == cur_pos && in_stream != null) return; if (httpConnection != null) { httpConnection.disconnect(); httpConnection = null; } if (in_stream != null) { in_stream.close(); in_stream = null; } httpConnection = (HttpURLConnection) url.openConnection(); httpConnection.setConnectTimeout(connect_timeout); httpConnection.setReadTimeout(read_timeout); String sProperty = "bytes=" + start_pos + "-"; httpConnection.setRequestProperty("Range", sProperty); //httpConnection.setRequestProperty("Connection", "Keep-Alive"); int responseCode = httpConnection.getResponseCode(); if (responseCode < 200 || responseCode >= 300) { try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } throw new IOException("HTTP responseCode="+responseCode); } in_stream = httpConnection.getInputStream(); cur_pos = start_pos; } }
二、IWriterCallBack接口
package instream; /* * 读/写通信接口.类似于C++的回调函数 * * 例: * class BuffRandAcceURL 内实现本接口的方法tryWriting() * class BuffRandAcceURL 内new Writer(this, ...)传值到Writer.icb * class Writer 内调用icb.tryWriting() */ public interface IWriterCallBack { public int tryWriting() throws InterruptedException; public void updateBuffer(int i, int len); public void updateWriterCount(); public int getWriterCount(); public void terminateWriters(); }
三、Writer类:下载线程,负责向buf[]写数据。
package instream; import java.net.URL; public final class Writer implements Runnable { private static boolean isalive = true; // 一个线程超时其它线程也能退出 private static byte[] buf; private static IWriterCallBack icb; private HttpReader hr; public Writer(IWriterCallBack cb, URL u, byte[] b, int i) { hr = new HttpReader(u); icb = cb; buf = b; Thread t = new Thread(this,"dt_"+i); t.setPriority(Thread.NORM_PRIORITY + 1); t.start(); } public void run() { int wbytes=0, wpos=0, rema = 0, retry = 0; int idxmask = BuffRandAcceURL.UNIT_COUNT - 1; boolean cont = true; int index = 0; //buf[]内"块"索引号 int startpos = 0; //index对应的文件位置(相对于文件首的偏移量) long time0 = 0; while (cont) { try { // 1.等待空闲块 if(retry == 0) { if ((startpos = icb.tryWriting()) == -1) break; index = (startpos >> BuffRandAcceURL.UNIT_LENGTH_BITS) & idxmask; wpos = startpos & BuffRandAcceURL.BUF_LENGTH_MASK; wbytes = 0; rema = BuffRandAcceURL.UNIT_LENGTH; time0 = System.currentTimeMillis(); } // 2.定位 hr.seek(startpos); // 3.下载"一块" int w; while (rema > 0 && isalive) { w = (rema < 2048) ? rema : 2048; //每次读几K合适? if ((w = hr.read(buf, wpos, w)) == -1) { cont = false; break; } rema -= w; wpos += w; startpos += w; // 能断点续传 wbytes += w; } // 下载速度足够快就结束本线程 long t = System.currentTimeMillis() - time0; if(icb.getWriterCount() > 1 && t < 500) cont = false; //4.通知"读"线程 retry = 0; icb.updateBuffer(index, wbytes); } catch (Exception e) { if(++retry == HttpReader.MAX_RETRY) { isalive = false; icb.terminateWriters(); break; } } } icb.updateWriterCount(); try { hr.close(); } catch (Exception e) {} hr = null; } }
四、BuffRandAcceURL类功能:创建下载线程;read方法从buf[]读数据。关键是如何简单有效防止死锁?
package instream; import java.net.URL; public final class BuffRandAcceURL implements IWriterCallBack { public static final int UNIT_LENGTH_BITS = 15; public static final int UNIT_LENGTH = 1 << UNIT_LENGTH_BITS; //2^16=32K public static final int BUF_LENGTH = UNIT_LENGTH << 4; public static final int UNIT_COUNT = BUF_LENGTH >> UNIT_LENGTH_BITS; //16块 public static final int BUF_LENGTH_MASK = (BUF_LENGTH - 1); private static final int MAX_WRITER = 5; private static long file_pointer; private static int read_pos; private static int fill_bytes; private static byte[] buf; //同时作写线程同步锁 private static int[] unit_bytes; //同时作读线程互斥锁 private static int alloc_pos; private static URL url; private static boolean isalive = true; private static int writer_count; private static long file_length; private static long frame_bytes; private static int free_unit = UNIT_COUNT; // "信号量"计数器 public BuffRandAcceURL(String sURL) throws Exception { this(sURL,MAX_WRITER); } public BuffRandAcceURL(String sURL, int download_threads) throws Exception { buf = new byte[BUF_LENGTH]; unit_bytes = new int[UNIT_COUNT]; url = new URL(sURL); // 创建"写"线程 writer_count = download_threads; for (int i = 0; i < download_threads; i++) { new Writer(this, url, buf, i + 1); Thread.sleep(200); } try_cache(); } /* * 缓冲 */ private void try_cache() throws InterruptedException { int cache_size = BUF_LENGTH; int bi = unit_bytes[read_pos >> UNIT_LENGTH_BITS]; if(bi != 0) cache_size -= UNIT_LENGTH - bi; while (fill_bytes < cache_size) { if (writer_count == 0 || isalive == false) return; synchronized (unit_bytes) { unit_bytes.wait(200); //wait(200)错过通知也可结束循环? } } } private int try_reading(int i, int len) throws Exception { int n = (i + 1) & (UNIT_COUNT - 1); int r = (unit_bytes[i] > 0) ? (unit_bytes[i] + unit_bytes[n]) : unit_bytes[i]; if (r < len) { if (writer_count == 0 || isalive == false) return r; try_cache(); } return len; } /* * 各个"写"线程互斥等待空闲块. 空闲块按由小到大的顺序分配. */ public int tryWriting() throws InterruptedException { int ret = -1; synchronized (buf) { while (free_unit == 0 && isalive) buf.wait(); if(alloc_pos >= file_length || isalive == false) return -1; ret = alloc_pos; alloc_pos += UNIT_LENGTH; free_unit--; } return ret; } /* * "写"线程向buf[]写完数据后调用,通知"读"线程 */ public void updateBuffer(int i, int len) { synchronized (unit_bytes) { unit_bytes[i] = len; fill_bytes += len; unit_bytes.notify(); } } /* * "写"线程准备退出时调用 */ public void updateWriterCount() { synchronized (unit_bytes) { writer_count--; unit_bytes.notify(); } } public int getWriterCount() { return writer_count; } /* * read方法内调用 */ public void notifyWriter() { synchronized (buf) { buf.notifyAll(); } } /* * 被某个"写"线程调用,用于终止其它"写"线程;isalive也影响"读"线程流程 */ public void terminateWriters() { synchronized (unit_bytes) { if (isalive) { isalive = false; System.out.println("\n读取文件超时。重试 " + HttpReader.MAX_RETRY + " 次后放弃,请您稍后再试。"); } unit_bytes.notify(); } notifyWriter(); } public int read(byte[] b, int off, int len) throws Exception { int i = read_pos >> UNIT_LENGTH_BITS; // 1.等待有足够内容可读 if(try_reading(i, len) < len || isalive == false) return -1; // 2.读取 int tail = BUF_LENGTH - read_pos; // write_pos != BUF_LENGTH if (tail < len) { System.arraycopy(buf, read_pos, b, off, tail); System.arraycopy(buf, 0, b, off + tail, len - tail); } else System.arraycopy(buf, read_pos, b, off, len); fill_bytes -= len; file_pointer += len; read_pos += len; read_pos &= BUF_LENGTH_MASK; unit_bytes[i] -= len; if (unit_bytes[i] < 0) { int ni = read_pos >> UNIT_LENGTH_BITS; unit_bytes[ni] += unit_bytes[i]; unit_bytes[i] = 0; free_unit++; notifyWriter(); } else if (unit_bytes[i] == 0) { free_unit++; // 空闲块"信号量"计数加1 notifyWriter(); // 3.通知 } // 如果下一块未填满,意味着文件读完,第1步已处理一次读空两块的情况 return len; } }
本文是JAVA开源项目jmp123源代码的一部分,单独成文旨在与朋友们交流文中提出的问题模型的解决方法。
【jmp123下载地址】http://jmp123.sourceforge.net/
评论
<div class="quote_div">最好能贴一下使用代码,否则真的很难有耐心看下去。 有无从入手的感觉 </div>
<p> </p>
<div class="quote_title">xiayh1002 写道</div>
<div class="quote_div">代码最好能下载运行一下,多线程光这么看代码,还比较难理解</div>
<p> </p>
<div class="quote_title">beginLi 写道</div>
<div class="quote_div">楼主 开源是有好处的..你的播放器就舍不得拿出来? </div>
<p> </p>
<p>这儿中介对以缓冲方式读取远程文件的方法的一介简要介绍。更详细的讲解在本站论坛和本站我的博客上有,如果你有兴趣,请看《(四)用JAVA编写MP3解码器——读取文件》</p>
<p>本站论坛 <a href="http://www.iteye.com/topic/740090">http://www.iteye.com/topic/740090</a></p>
<p>本站博客 <a href="http://lfp001.iteye.com/blog/740090">http://lfp001.iteye.com/blog/740090</a></p>
+1.变量命名不符java的规范。
发表评论
-
(附)用JAVA编写MP3解码器——GUI
2010-12-24 19:32 4247以下代码是开源(GPL)程序jmp123的一部分。 ... -
(十八)用JAVA编写MP3解码器——迷你播放器
2010-08-30 18:01 56001.定义解码一帧的接口 ILayer123 ... -
(十七)用JAVA编写MP3解码器——解码Layer1
2010-08-29 14:32 3774Layer1的编码更简单,解码端的代码也就比La ... -
(十六)用JAVA编写MP3解码器——解码Layer2
2010-08-29 14:17 4051MPEG 1.0/2.0/2.5 对声音的压缩分 ... -
(十五)用JAVA编写MP3解码器——音频输出
2010-08-29 13:40 5256解码一帧Layer3第10步:音频输出 -- class ... -
(十四)用JAVA编写MP3解码器——多相合成滤波
2010-08-29 10:03 3493解码一帧Layer3第8步:多相频率倒置 (Inverse ... -
(十三)用JAVA编写MP3解码器——IMDCT快速算法
2010-08-28 18:14 2829解码一帧Layer3第7步:IMDCT和子带混合 -- cl ... -
(十二)用JAVA编写MP3解码器——消混叠处理
2010-08-26 19:37 2394解码一帧Layer3第6步:消混叠处理 -- class L ... -
(十一)用JAVA编写MP3解码器——立体声处理
2010-08-25 14:32 2803解码一帧Layer3第5步: ... -
(十)用JAVA编写MP3解码器——逆量化和重排序
2010-08-22 15:20 3068解码一帧Layer3第4步:逆量化和重排序 -- requa ... -
(九)用JAVA编写MP3解码器——哈夫曼解码
2010-08-21 16:21 3354解码一帧Layer3第3步:哈夫曼解码 -- huffman ... -
(八)用JAVA编写MP3解码器——解码增益因子
2010-08-20 15:39 2292解码一帧Layer3第2步:解码增益因子 -- getSc ... -
(七)用JAVA编写MP3解码器——解码帧边信息
2010-08-20 11:39 2635解码一帧Layer3第1步: ... -
(六)用JAVA编写MP3解码器——帧数据结构
2010-08-19 20:39 3324MP3文件按帧(frame)依次存储,解码时也是 ... -
(五)用JAVA编写MP3解码器——解析文件信息
2010-08-19 11:34 4840前文提到解析MP3标签,程序源码中也已经出现了调 ... -
(四)用JAVA编写MP3解码器——读取文件
2010-08-18 13:28 35621.随机文件访问接口 对MP3解码时需要随机读 ... -
(三)用JAVA编写MP3解码器——读取位流
2010-08-17 23:17 3406文件以字节为单位读取,MP3解码器输入的数据是位流,即每 ... -
(二)用JAVA编写MP3解码器——帧头信息解码
2010-08-17 21:43 65071.解析帧头 帧头共4字节,从高位到低位这32比特的含 ... -
(一)用JAVA编写MP3解码器——前言
2010-08-17 21:04 18854【内容提要】 《用JAVA编写MP3解码器》系列文章将 ... -
JAVA实现位流缓冲区
2009-06-22 11:03 683原理很简单,关键是效率。以流媒体解码为例,一次读几比特 ...
相关推荐
设计了一个环形字节缓冲区类,可用于通信数据收发的缓冲处理。类的读写及查看接口已经加上了多线程同步访问锁
C语言实现环形缓冲区,可供多线程读写操作
采用2个producer和一个Consumer把整数放入到(或者从中取出)一个环形缓冲Circle Buffer中,这块连续的存储会被反复使用。一个写指针指向数据写入的位置,如果写到Buffer的末端,则从Buffer的头端开始写。类似的,一...
设计了一个环形字节缓冲区类,可用于通信数据收发的缓冲处理。类的读写及查看接口已经加上了多线程同步访问锁。
环形缓冲,支持主动读取和读取事件两种模式。 mysql链接池,使用占位符(?)方式生成sql语句,支持同步异步操作。 简单易用的ssl加解密黑盒,支持多线程。 其他一些有用的工具。 命令行解析工具,可以很便捷的实现...
本实验的多个缓冲区不是环形循环的,也不要求按顺序访问。生产者可以把产品放到目前某一个空缓冲区中。 消费者只消费指定生产者的产品。 在测试用例文件中指定了所有的生产和消费的需求,只有当共享缓冲区的数据...
(2)了解Windows 2000/XP中多线程的并发执行机制,线程间的同步和互斥。 (3)学习使用Windows2000/XP中基本的同步对象,掌握相应的API。 2、实验要求 (1)创建生产者和消费者线程 在Windows2000环境下,创建一个...
读取捕获文件和实时跟踪,包括内核环形缓冲区。 可配置的数据包过滤器。 限制速率,压缩和输出文件轮换的无效数据包的Pcap转储。 可配置的CBOR和CSV输出(针对Impala / hadoop导入,不兼容RFC 4180)和可选的二...
由环形缓冲区实现的读写缓冲区的动态扩展 异步读写 SO_REUSEPORT端口重用支持 自动清理空闲连接 支持WebSocket / Protobuf 支持计划任务,延迟任务 支持自定义协议 网络模型 gev仅使用一些goroutine,其中一个侦听...
多核多线程、生产消费者模型、读写者模型、多线程无锁环形队列及LockFreeList概念 Socket重用、端口重用 心跳、粘包、乱序 ------------------------我收集的文章及源码的部分目录---------------------- --------...
多核多线程、生产消费者模型、读写者模型、多线程无锁环形队列及LockFreeList概念 Socket重用、端口重用 心跳、粘包、乱序 ------------------------我收集的文章及源码的部分目录---------------------- --------...
1. 多目标文件的链接 2. 定义和声明 2.1. extern和static关键字 2.2. 头文件 2.3. 定义和声明的详细规则 3. 静态库 4. 共享库 4.1. 编译、链接、运行 4.2. 动态链接的过程 4.3. 共享库的命名惯例 5. 虚拟内存管理 21...