Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2012
    Posts
    23
    Rep Power
    0

    Default Illegal Thread State Exception!

    My Code is as follows:

    class Example1 implements Runnable {
    Thread t = new Thread(this,"Running Thread:");

    public void run(){
    t.start();
    System.out.println("Running Thread:" + t);
    }
    }




    class ThreadExample{

    public static void main(String args[]){
    Example1 X1 = new Example1();

    Thread.currentThread().setName("Main Thread");


    try{
    System.out.println(Thread.currentThread().getName( ));
    X1.run();
    }
    catch (IllegalThreadStateException e) {
    System.out.println("Illegal Thread State Exception");
    }


    }
    }

    It is compiling fine but is giving an error while running the program...
    Main Thread
    Running Thread:Thread[Running Thread:,5,main]
    Exception in thread "Running Thread:" java.lang.IllegalThreadStateException
    at java.lang.Thread.start(Thread.java:656)
    at Example1.run(ThreadExample.java:5)
    at java.lang.Thread.run(Thread.java:680)

    Can anyone please explain?

  2. #2
    christopherx is offline Member
    Join Date
    Oct 2011
    Posts
    92
    Rep Power
    0

    Default Re: Illegal Thread State Exception!

    Java Code:
    class Example1 implements Runnable {
    Thread t = new Thread(this,"Running Thread:");
    
    public void run(){
    t.start();
    System.out.println("Running Thread:" + t);
    }
    }
    
    
    
    
    class ThreadExample{
    
    public static void main(String args[]){
    Example1 X1 = new Example1();
    
    Thread.currentThread().setName("Main Thread");
    
    
    try{
    System.out.println(Thread.currentThread().getName( ));
    X1.run();
    }
    catch (IllegalThreadStateException e) {
    System.out.println("Illegal Thread State Exception");
    }
    
    
    }
    }
    Please remember to use the code tags in future :)

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Illegal Thread State Exception!

    Read the API for Thread. You can't start() the same Thread more than once.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Join Date
    Feb 2012
    Posts
    23
    Rep Power
    0

Similar Threads

  1. Replies: 4
    Last Post: 09-16-2011, 10:08 PM
  2. Replies: 2
    Last Post: 02-14-2011, 02:27 PM
  3. Thread at wait() state , shared data (volatile? confused ;/ )
    By doomsword2001 in forum Threads and Synchronization
    Replies: 5
    Last Post: 02-09-2011, 03:11 AM
  4. Replies: 4
    Last Post: 10-25-2010, 07:42 PM
  5. Replies: 3
    Last Post: 11-06-2008, 04:24 PM

Posting Permissions

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