JAVA 数据结构之Queue处理实例代码
程序员文章站
2024-03-06 20:11:32
java queue处理
实例代码:
import java.util.linkedlist;
import java.util.queue;
privat...
java queue处理
实例代码:
import java.util.linkedlist; import java.util.queue; private static queue<framestruct> framequeue = new linkedlist<framestruct>(); private static lock lock = new reentrantlock(); private playerthread p = new playerthread();
从队列取数据进行处理:
private class playerthread extends thread { @override public void run() { framestruct frame; while(bplayrun) { if(bcanflush) { lock.lock(); while((frame=framequeue.poll())!=null) { onframe(frame.buf, 0, frame.len); try { thread.sleep(30); } catch (interruptedexception e) { } } lock.unlock(); } } } }
另一线程将数据放入队列:
framestruct frame = new framestruct(); frame.buf = new byte[bytecount]; frame.len = bytecount; system.arraycopy(framedata, 0, frame.buf, 0, bytecount); lock.lock(); framequeue.offer(frame); lock.unlock();
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!