6 Replies Latest reply on Apr 15, 2009 2:39 PM by r035198x

    Illegal thread state exception

    843789
      Hullo,

      For the following, where all I intend to do is create a small time delay, I get the
      java.lang.IllegalThreadStateException
           at java.lang.Thread.start(Unknown Source)
      Here is what I was trying to do :
      public static void main(String[] args){
         try{
      
         test test = new test();
         for(int i = 0;i<5;i++){
      
         test.start();
         Thread.sleep(3000);
         }
         }
         catch(Exception e){
         e.printStackTrace();
         }
      Could you please help me in eliminating this exception ?

      cheers,
        • 1. Re: Illegal thread state exception
          843789
          Check the JavaDoc for IllegalThreadStateException and see when it occurs.

          Hint: you can't start the same thread more than once.
          • 2. Re: Illegal thread state exception
            843789
            JoachimSauer wrote:
            Check the JavaDoc for IllegalThreadStateException and see when it occurs.

            Hint: you can't start the same thread more than once.
            Thanks for your reply. I looked up the doc and it said
            Thrown to indicate that a thread is not in an appropriate state for the requested operation. See, for example, the suspend and resume methods in class Thread. 
            Now, the above obviously is just a small replica of what I am doing.
            I have a queue which has in it some orderservice threads which need to be started.
            It works like a charm for a queue lesser than 50 orders, but after that it explodes and cant semd mails.
            So I put in a counter for the no. of items in the queue and before starting a thread I let it sleep for a progressive number of counter*10 milliseconds before the next call to start. and this is resulting in the illegalthreadstateexception. Any ideas on how I could work around this ? there is no way I can manage this on the server side because this is a simple mail sending program.

            cheers,
            • 3. Re: Illegal thread state exception
              r035198x
              Read the concurrency section in Sun's tutorial. Most of the things you said don't make sense.
              Understand what the start and sleep methods do before trying to use them.
              • 4. Re: Illegal thread state exception
                843789
                Ok, now I understand my mistake
                Sleep will :
                Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers. The thread does not lose ownership of any monitors. 
                However, what I thought I was doing was, that I was giving the thread that already began to finish up before the next thread comes into the picture, because an x no. of threads started at one point, somehow, are failing with error messages pertaining to the java mail.
                So, I think using this should be helpful :

                join()
                               OrderService.orderServiceThread = (Thread) ordersIterator.next();
                               OrderService.orderServiceThread .start();
                               OrderService.orderServiceThread .join();
                instead of
                               OrderService.orderServiceThread = (Thread) ordersIterator.next();
                               OrderService.orderServiceThread .start();
                               OrderService.orderServiceThread.sleep(100); 
                Will this be a right way to do it ?


                cheers..
                • 5. Re: Illegal thread state exception
                  843789
                  When you do this:
                  someThread.start();
                  someThread.join();
                  Then you're starting a thread and immediately wait for that thread to finish.

                  This is pretty much the same thing as if you didn't use threads at all (since only 1 thread will be working at any given time), so you gain nothing from it.

                  You'll want to start all threads that do work, and then try to .join() on all threads.

                  But I suggest you use a higher-level abstractions such as the ThreadPoolExecutor, where you can just call .submit() to queue any tasks and they will be executed when there's an idle thread in the pool.
                  • 6. Re: Illegal thread state exception
                    r035198x
                    No. Think about which thread will call the join method.
                    If the threads must all execute in sequence then their tasks don't need to be put into separate threads of execution at all.