Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can solve this using <code>Blocking queues</code>, which supports the producer consumer design pattern. The <a href="http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/BlockingQueue.html" rel="nofollow">BlockingQueue</a> will be your "to do" list, where the <code>Producer</code> will place data (the <code>byte[]</code>) as it becomes available and the <code>Consumer</code> retrieves data(the <code>byte[]</code>) from the queue when it is ready to work with the data. </p> <p>Create a <code>Producer</code> which can check continuosly for a <code>byte[]</code> received via bluetooth:</p> <pre><code>class Producer implements Runnable { private final BlockingQueue&lt;byte[]&gt; byteArrayQueue; public Producer(BlockingQueue&lt;byte[]&gt; byteArrayQueue) { this.byteArrayQueue = byteArrayQueue; } @Override public void run() { // Place your data into the queue // byteArrayQueue.put(GlobalVar.bt_input);//put received value into the queue } } </code></pre> <p>And this is the <code>Consumer</code> that checks for available data in the <code>queue</code> and will work on it:</p> <pre><code>class Consumer implements Runnable { private final BlockingQueue&lt;byte[]&gt; byteArrayQueue; public Consumer(BlockingQueue&lt;byte[]&gt; byteArrayQueue) { this.byteArrayQueue = byteArrayQueue; } @Override public void run() { try { while (true) { byte[] take = byteArrayQueue.take(); // DO YOUR WORK } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } } </code></pre> <p>Now start your <code>Producer</code> and <code>Consumer</code> :</p> <pre><code>BlockingQueue&lt;byte[]&gt; queue = new LinkedBlockingQueue&lt;byte[]&gt;(1); new Thread(new Producer(queue)).start(); new Thread(new Consumer(queue)).start(); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload