Results 1 to 3 of 3
  1. #1
    Join Date
    May 2010
    Posts
    22
    Rep Power
    0

    Default how to reduce the thread sleep time and wake up the thread

    the thread time is above 3000 , how to wake up the thread for particular time period

    Java Code:
    package tcplistener;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.Properties;
    import java.util.Timer;
    import java.util.TimerTask;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class PortListener extends HttpServlet {
       private Socket sock;
       private ServerSocket serverSocket;
       private Thread t ;
       private Timer timer;
       private int a = 0;
    
        
    public void init() throws ServletException
    
    {
        try{
                timer = new Timer();
                timer.schedule(new ToDoTask(), 5000);
    
    
        }catch(Exception ex)
        {
           Logger.getLogger(PortListener.class.getName()).log(Level.SEVERE, null, ex) ;
        }
    }
    
    class ToDoTask extends TimerTask
     {
       public void run()
        {
           int port = 0;
           try
            {
                    timer.cancel();
                    InputStream is = getServletContext().getResourceAsStream("/WEB-INF/navl.properties");
                    Properties  p  = new Properties();
                    p.load(is);
                    is.close();
                    String pro = p.getProperty("dataPort");
                    port = Integer.parseInt(pro);
                    serverSocket = new ServerSocket(port);
    
            while (true)
            {
    
    
                System.out.println("Listening on ...... " + port);
                sock = serverSocket.accept();
                synchronized(this)
                {
                        t = new Thread(new ModuleHandler(sock));
                        t.start();
                }
                }
    
            }
            catch(Exception ee)
           {
           
           }
        }
    
    
    
      }
    
    
    
        protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    
       }
    
    
    
    
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
            processRequest(request, response);
        }
    
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
            processRequest(request, response);
        }
    
            @Override
        public String getServletInfo() {
            return "Short description";
        }
    }
    Any wrong in the above code please help me any body having idea

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    To wake up a sleeping thread, use the interrupt() method.

    I don't see where in the code you want to disturb the sleeping thread.
    In face I don't see an calls to sleep().

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Difference between Thread.yield() and Thread.sleep() methods
    By Nageswara Rao Mothukuri in forum New To Java
    Replies: 12
    Last Post: 07-30-2010, 05:37 PM
  2. Do I have to put Thread to sleep?
    By atch in forum New To Java
    Replies: 0
    Last Post: 03-08-2010, 10:21 AM
  3. thread.sleep
    By jc5 in forum CLDC and MIDP
    Replies: 0
    Last Post: 09-02-2009, 11:51 PM
  4. Sleep in thread
    By jithan in forum New To Java
    Replies: 1
    Last Post: 08-27-2008, 02:27 PM
  5. How to use the sleep and thread?
    By jiuhu in forum Java Applets
    Replies: 4
    Last Post: 08-07-2007, 02:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •