Queue processing instance code for JAVA data structures

  • 2020-06-15 08:25:42
  • OfStack

java Queue processing

Example code:


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();

Fetch data from the queue for processing:


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();
        }
      }
    }
  }

Another thread puts the data into the queue:


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();

Thank you for reading, I hope to help you, thank you for your support to this site!


Related articles: