title: How to create a thread that runs all the time my application is running
body: <p>EDIT:
  I'm now sure that the problem is related to the 
      <code>while (true)</code> loop holding all other commands as I've commented it out and the application deploys without the attached exception. I'm not sure how much it is important but my <code>ServletContextListener</code> implementation looks like this:</p>
  
  <p><code>
           public class BidPushService implements ServletContextListener{</p>
  
  <pre><code>public void contextInitialized(ServletContextEvent sce) {   
  //Some init code not relevant, omitted for clarity
        BidPushThread t= new BidPushThread();
        t.setServletContext(sce.getServletContext());
        t.run();
  }
  </code></pre>
  
  <p></code></p>
  
  <p>So now the thread is run when the app is deployed but because the <code>while</code> loop is commented it has no real meaning.</p>
  
  <p>I need to have a thread run in the background when my application loads and constantly (without a timeout) check a certain queue for objects.  Of course, once there are objects, it "takes care of them" and then continues to check the queue.</p>
  
  <p>Currently, I'm implementing the <code>ServletContextListener</code> interface and I'm being called when the app loads.  In it, I do a few maintenance things and start a thread which I inherited from <code>java.lang.Thread</code>.</p>
  
  <p>Here is where my problem begins (or so I think).  In my <code>run()</code> method, I have a</p>
  
  <pre><code>while (true) {
      //some code which doesn't put the thread to sleep ever
  }
  </code></pre>
  
  <p>When I try to deploy my app to the server I get a <code>java.util.concurrent.TimeOutException</code>.
  What am I doing wrong?</p>
  
  <p>Can't I have a thread which is always running?  When the app is removed, that thread is stopped by the corresponding event in my <code>ServletContextListener</code>.</p>
  
  <p>I really do need something that keeps on checking the queue without delay.</p>
  
  <p>Thanks a lot for any help!</p>
  
  <p>Edit: This is the stack trace</p>
  
  <pre><code>GlassFish: deploy is failing=
      java.util.concurrent.TimeoutException
      at java.util.concurrent.FutureTask$Sync.innerGet(Unknown Source)
      at java.util.concurrent.FutureTask.get(Unknown Source)
      at com.sun.enterprise.jst.server.sunappsrv.SunAppServerBehaviour.publishDeployedDirectory(SunAppServerBehaviour.java:710)
      at com.sun.enterprise.jst.server.sunappsrv.SunAppServerBehaviour.publishModuleForGlassFishV3(SunAppServerBehaviour.java:569)
      at com.sun.enterprise.jst.server.sunappsrv.SunAppServerBehaviour.publishModule(SunAppServerBehaviour.java:266)
      at org.eclipse.wst.server.core.model.ServerBehaviourDelegate.publishModule(ServerBehaviourDelegate.java:948)
      at org.eclipse.wst.server.core.model.ServerBehaviourDelegate.publishModules(ServerBehaviourDelegate.java:1038)
      at org.eclipse.wst.server.core.model.ServerBehaviourDelegate.publish(ServerBehaviourDelegate.java:872)
      at org.eclipse.wst.server.core.model.ServerBehaviourDelegate.publish(ServerBehaviourDelegate.java:708)
      at org.eclipse.wst.server.core.internal.Server.publishImpl(Server.java:2690)
      at org.eclipse.wst.server.core.internal.Server$PublishJob.run(Server.java:272)
      at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)
  </code></pre>
  
  <p>My Code:</p>
  
  <pre><code>public class BidPushThread extends Thread {
      private ServletContext sc=null;
      @Override
      public void run() {
          if (sc!=null){
              final Map&lt;String, List&lt;AsyncContext&gt;&gt; aucWatchers = (Map&lt;String, List&lt;AsyncContext&gt;&gt;) sc.getAttribute("aucWatchers");
              BlockingQueue&lt;Bid&gt; aucBids = (BlockingQueue&lt;Bid&gt;) sc.getAttribute("aucBids");
  
                Executor bidExecutor = Executors.newCachedThreadPool(); 
                final Executor watcherExecutor = Executors.newCachedThreadPool();
                while(true)
                {  
                   try // There are unpublished new bid events.
                   {
                      final Bid bid = aucBids.take();
                      bidExecutor.execute(new Runnable(){
                         public void run() {
                            List&lt;AsyncContext&gt; watchers = aucWatchers.get(bid.getAuctionId()); 
                            for(final AsyncContext aCtx : watchers)
                            {
                               watcherExecutor.execute(new Runnable(){
                                  public void run() {
                                     // publish a new bid event to a watcher
                                     try {
                                      aCtx.getResponse().getWriter().print("A new bid on the item was placed. The current price "+bid.getBid()+" , next bid price is "+(bid.getBid()+1));
                                  } catch (IOException e) {
                                      // TODO Auto-generated catch block
                                      e.printStackTrace();
                                  }
                                  };
                               });
                            }                           
                         }
                      });
                   } catch(InterruptedException e){}
                }
  
          }
      }
      public void setServletContext(ServletContext sc){
          this.sc=sc;
      }
  }
  </code></pre>
  
  <p>Sorry for the formatting mess but for the life of my "indent code by 4 spaces" just does not work for me
  Edit: Read about 'BlockingQueue' and implemented it, but I'm still getting the exact same exception and stack trace. changed the above code to reflect the use of 'BlockingQueue'</p>