Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Create a new thread with a runnable and make sure it knows your server because your server should know all clients. If a client sends a message parse it through the data processor thread and let it do it's job. When it's done processing your task then let the server know so he can update all clients. </p> <p>Tip: you should make a waiting queue for the processing thread with something like a <a href="http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/LinkedBlockingQueue.html" rel="nofollow">LinkedBlockingQueue</a> so you can always put tasks on the queue without waiting for the task to finish. Then the thead will wait for things on the queue that need to be processed. This way the processing thread will only use CPU resources when there are actually tasks on the queue</p> <p>Here is a code example</p> <pre><code>public abstract class Queue implements Runnable { private final LinkedBlockingQueue&lt;Message&gt; queue; public Queue() { this.queue = new LinkedBlockingQueue&lt;Message&gt;(); } /** * Adds a message to the queue. * @param message */ public void add(final Message message) { try { queue.put(message); } catch (final InterruptedException e) { e.printStackTrace(); } } /** * Waits for new messages */ @Override public void run() { while(true) { try { final Message message = queue.take(); processMessage(message); } catch (final InterruptedException e) { e.printStackTrace(); } } } /** * Processes the new message */ protected abstract void processMessage(Message message); </code></pre> <p>}</p>
 

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