title: Java: Publisher-Observer pattern: High CPU Usage
body: <p>I have a ServerSocket Thread that accepts a connection and starts a socket handler. This part seems to be working fine with no memory leaks or high cpu usage. I added a Publisher thread and an observer thread and now, my java program is reporting high CPU usage.</p>
  <p>Subject.java:</p>
  <pre><code>public interface Subject {
  public void attach(Observer o);
  public void detach(Observer o);
  public void notifyUpdate(Message m);
  </code></pre>
  <p>}</p>
  <p>MessagePublisher.java:</p>
  <pre><code>public class MessagePublisher extends Thread implements Subject{
  private List&lt;Observer&gt; observers = new ArrayList&lt;&gt;();
  private boolean readyToPublish = false;
  private ConcurrentLinkedQueue&lt;Message&gt; msgHolder;
  public MessagePublisher(ConcurrentLinkedQueue&lt;Message&gt; _queue){
      this.msgHolder = _queue;
  }
  
  
  @Override
  public void attach(Observer o) {
      observers.add(o);
  }
  
  @Override
  public void detach(Observer o) {
      observers.remove(o);
  }
  
  @Override
  public void notifyUpdate(Message m) {
      for(Observer o: observers) {
          o.update(m);
      }
  }
  
  public void run(){
      this.readyToPublish = true;
      while (readyToPublish)
          {
              try
              {                    
                  Message _m = (Message)this.msgHolder.poll();
                  if(!_m.equals(null)){
                      System.out.println(&quot;Polled message: &quot; + _m.getMessage());
                      System.out.println(&quot;Number of subscribers: &quot; + observers.size());
                      notifyUpdate(_m);
                  }                    
                  
              }
              catch(Exception j) { }
              try { sleep(9); }
              catch(Exception e) { }
          }
          EndWork();
  }
  public void EndWork(){
      this.readyToPublish = false;
      this.observers.clear();
      
  }
  </code></pre>
  <p>}</p>
  <p>Main.java:</p>
  <pre><code>public static void main(String[] args) {
      // TODO code application logic here
      ConcurrentLinkedQueue&lt;Message&gt; msgHolder = new ConcurrentLinkedQueue&lt;Message&gt;();
      ServerSocketThread _socketThread = new ServerSocketThread(msgHolder);
      _socketThread.start();
      
      MessagePublisher _publisher = new MessagePublisher(msgHolder);
      _publisher.start();
      
      UIServerSocketThread _uiSocketThread = new UIServerSocketThread(_publisher);
      _uiSocketThread.start();        
      
  }
  </code></pre>
  <p>UIServerSocketThread.java:</p>
  <pre><code>public class UIServerSocketThread extends Thread{
  private ServerSocket        objServerSocket; 
  private Socket              objSocket;
  private int                 iPort = 21001;
  private FileHandler         obj_LogFileHandler;
  private Logger              obj_Logger;
  
  private int file_size =   8000000;
  private int numLogFiles  = 20;
  
  private UIClientSocketThread  objCltSocket;
  private MessagePublisher    objPublisher;
  private boolean             running = false;
  public UIServerSocketThread(MessagePublisher _publisher){
      
      this.running = true;
      try {            
          this.obj_LogFileHandler = new FileHandler(&quot;uiserver.log.%g&quot;, file_size, numLogFiles);
          this.obj_LogFileHandler.setFormatter(new SimpleFormatter());
      }
      
      catch ( IOException obj_Exception ) {
          this.obj_LogFileHandler = null;
      }
      catch ( SecurityException obj_Exception ) {
          this.obj_LogFileHandler = null;
      }
  
      this.obj_Logger = null;
  
      if ( this.obj_LogFileHandler != null ) {
          this.obj_Logger = Logger.getLogger(&quot;eti.logger.uiserver&quot;);
          this.obj_Logger.addHandler(this.obj_LogFileHandler);
      }
      try {
      this.objServerSocket = new ServerSocket(this.iPort);
      } catch(IOException i){
          //i.printStackTrace();
          this.obj_Logger.info(i.getMessage());
      }
      this.objPublisher = _publisher;
  }
  
  public void run() {        
      StringBuffer        sMsg;
      
      sMsg = new StringBuffer();        
      
      while ( this.running ) {
          try {
              this.objSocket = this.objServerSocket.accept();
          } catch(Exception e){
              sMsg.append(&quot;Error accepting ui socket connection\n&quot;);
              sMsg.append(e.getMessage());                
              this.obj_Logger.info(sMsg.toString());
          }
          
          try {
                  
                  this.objCltSocket = new UIClientSocketThread(this.objSocket, this.obj_Logger);
                  if(!this.objPublisher.equals(null)){
                      this.obj_Logger.info(&quot;Attacing clientObserver&quot;);
                      this.objPublisher.attach(this.objCltSocket);
                  }
                  
                  this.objCltSocket.start();
              
          } catch(Exception r) {
              sMsg.append(&quot;Error \n&quot;);
              sMsg.append(r.getMessage());                
              this.obj_Logger.info(sMsg.toString());
          }   
          
      }
      this.objPublisher.EndWork();
      stopServerSocketThread();
          
  } // end run
  public void stopServerSocketThread() {
       try {
           this.running = false;
          this.objServerSocket.close();
          this.objServerSocket = null;
          this.obj_Logger.info(&quot;Server NOT ACCEPTING Connections!!&quot;);
      } catch(Exception e ) {            
          this.obj_Logger.info(e.getMessage());
      }
  }
  </code></pre>
  <p>}</p>
  <p>I am not sure where the issue is. Any help is appreciated.</p>