title: Java / Prioritization of threads
body: <p>I have an application that starts four threads to listen on incoming packets. Every thread opens a socket on a different port. Normally packets are received only on one port at the time, but in some cases messages can be received on two ports for some seconds. Every of these threads processes the messages and updates a bunch of listeners (all of them are doing some Swing painting stuff). As the messages are sent with a frequency of 10 Hz and the painting actions on the Swing components take some time, my first approach was to process only one messages out of 20 (2 seconds time to finish the paint on the components). Works well...</p>
  
  <p>But when receiving two messages at the time, I need to tell my application just to process one of them (the one received only for short time). In summary 10 messages are received on the 2nd port, also with a frequency of 10 Hz. Means, using the first approach sometimes I miss all 10 of them, because only one out of 20 is processed.</p>
  
  <p>Whenever a messages on the 2nd port is received I want my application to process that one, doesn't matter what is received on the 1st port or if something is painted at that time.</p>
  
  <p>The following code shows the implementation of my threads, four of these are started at the same time depending on the ports given through the constructor.</p>
  
  <pre><code>private class IncomingRunner implements Runnable {
  
      private int listenPort;
      private DatagramSocket localSocket;
      private DatagramPacket packet;
      private int counter = 0;
  
      public IncomingRunner(int port) {
          this.listenPort = port;
      }
  
      @Override
      public void run() {
          try {
              localSocket = new DatagramSocket(listenPort);
  
              byte[] buffer = new byte[1024];
              packet = new DatagramPacket(buffer, buffer.length);
  
              while(isRunning)
                  recvIncomingMsg();
  
          } catch (SocketException e) {
              e.printStackTrace();
          }
      }
  
      private void recvIncomingMsg() {
          try {
              localSocket.receive(packet);
              port = localSocket.getLocalPort();
  
              ReceivedMsg eventMsg;
  
              if(port == Config.PORT_1) {
                  eventMsg = new ReceivedMsg(Config.PORT_1, Config.SOMETHING_1);
                  System.out.println(HexWriter.getHex(packet.getData()));
              } else if (port == Config.PORT_2) {
                  eventMsg = new ReceivedMsg(Config.PORT_2, Config.SOMETHING_2);
                  System.out.println(HexWriter.getHex(packet.getData()));
              } else if (port == Config.PORT_3) {
                  eventMsg = new ReceivedMsg(Config.PORT_3, Config.SOMETHING_3);
                  System.out.println(HexWriter.getHex(packet.getData()));
              } else {
                  eventMsg = new ReceivedMsg(Config.PORT_4, Config.SOMETHING_4);
                  System.out.println(HexWriter.getHex(packet.getData()));
              }
  
              counter++;
  
              if(counter%20 == 0) {
                  forward2PacketPanel(eventMsg);
                  counter = 0;
              }
  
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  
      private void forward2PacketPanel(final ReceivedMsg t) {
          for(final IPacketListener c : listeners) {
              if(c instanceof IPacketListener) {
                  new Thread(new Runnable() {
                      @Override
                      public void run() {
                          ((IPacketListener)c).update(t);
                      }
                  }).start();
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>UPDATE:</strong>
  The reason why I am starting new Threads to update the listeners is, because all of them should update the GUI at the same time. Every <code>updated</code> calls a <code>paintComponent()</code> method on a different <code>JPanel</code>. So all of them should run together.</p>
  
  <p><strong>UPDATE2:</strong>
  I cannot use the first approach as this causes messages loss of maybe important messages (received on 2nd port). What I need is, when a normal Msg is received just process it and do the painting, doesn't matter how many new normal messages (on 1st port) come in. But even if only one Msg on 2nd port is received, the application needs to process that one, regardless what is going on in the normal receiver thread.</p>
  
  <p>I guess I am facing two problems here:</p>
  
  <ol>
  <li><p>I need to make each thread waiting until the painting is finished, as that is UDP I can process a normal packet, and forget about all following normal packets, during the painting actions. When done, process the next normal packet.</p></li>
  <li><p>If a packet on 2nd port is received, break all normal packet processing actions and do the things needed to process the special packet.</p></li>
  </ol>
  
  <p>Problem (1) is solved using a BitSet in the MainIncomingClass. Every Listener uses some kind of callback function to indicate that its done with painting and sets a specific Bit in the BitSet. If not all are true, I do not process any new Packet, just let them go.</p>