Hi all,

I am having a problem with threads as demonstrated below:

I have four very small and simple files. They are as follows:

1) public class MainClass {
	private static SchedulerTest sch = null;
	public static void main(String[] args) {
		try{
			sch = new SchedulerTest();
			ThreadController tc = new ThreadController(10);
			System.out.println("Starting the ThreadController from main!");
			tc.runTest();
			Thread.sleep(10000);
			System.out.println("Ending Program!");
		}catch(Exception e){
			e.printStackTrace();
		}
		return;
	}
	
	public static synchronized void startSchedulerTest(){
		try{			
			sch.start();	
		}catch(Exception e){
			e.printStackTrace();
		}	
		return;
	}
}


2) public class ThreadController {
	TestThread[] threads = null;
	public ThreadController(int arg0) {		
		super();
		threads = new TestThread[arg0];		
		System.out.println("Started ThreadController: creating TestThreads!");
		try{
			for(int i = 0; i < arg0; i++){
				threads[i] = new TestThread("Testthread " + (i+1) + "created!");
			}
		}catch(Exception e){
			e.printStackTrace();
		}		
	}
	
	public void runTest(){
		try{
			for(int i = 0; i < threads.length; i++){
				threads[i].start();	
				//Thread.sleep(2000);			
			}
			System.out.println("Started all TestThreads! Now sleeping in ThreadController!");
			Thread.sleep(5000);
			System.out.println("Woke up in ThreadController! Joining TestThreads...");
			for(int i = 0; i < threads.length; i++){
				threads[i].join();				
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}

3) public class TestThread extends Thread {

	public TestThread() {
		super();
	}

	public TestThread(String arg0) {
		super(arg0);		
		System.out.println("Hello " + arg0);		
	}
	
	public void run(){
		try{
			Thread.sleep(1000);			
			MainClass.startSchedulerTest();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}

4) public class SchedulerTest extends Thread {
	
	public SchedulerTest(){
		System.out.println("Created SchedulerTest!");
	}
	
	public void run(){
		try{
			System.out.println("In run method of SchedulerTest!");
			Thread.sleep(1000);
		}catch(Exception e){
			e.printStackTrace();
		}
		return;		
	}

}

Whenever I try to run the MainClass, I get an "java.lang.IllegalThreadStateException" exception.

The output is as follows:

A:\>java MainClass
Created SchedulerTest!
Started ThreadController: creating TestThreads!
Hello Testthread 1created!
Hello Testthread 2created!
Hello Testthread 3created!
Hello Testthread 4created!
Hello Testthread 5created!
Hello Testthread 6created!
Hello Testthread 7created!
Hello Testthread 8created!
Hello Testthread 9created!
Hello Testthread 10created!
Starting the ThreadController from main!
Started all TestThreads! Now sleeping in ThreadController!
In run method of SchedulerTest!
java.lang.IllegalThreadStateException
at java.lang.Thread.start(Native Method)
at MainClass.startSchedulerTest(MainClass.java:31)
at TestThread.run(TestThread.java:33)
java.lang.IllegalThreadStateException
at java.lang.Thread.start(Native Method)
at MainClass.startSchedulerTest(MainClass.java:31)
at TestThread.run(TestThread.java:33)
java.lang.IllegalThreadStateException
at java.lang.Thread.start(Native Method)
at MainClass.startSchedulerTest(MainClass.java:31)
at TestThread.run(TestThread.java:33)
java.lang.IllegalThreadStateException
at java.lang.Thread.start(Native Method)
at MainClass.startSchedulerTest(MainClass.java:31)
at TestThread.run(TestThread.java:33)
java.lang.IllegalThreadStateException
at java.lang.Thread.start(Native Method)
at MainClass.startSchedulerTest(MainClass.java:31)
at TestThread.run(TestThread.java:33)
java.lang.IllegalThreadStateException
at java.lang.Thread.start(Native Method)
at MainClass.startSchedulerTest(MainClass.java:31)
at TestThread.run(TestThread.java:33)
java.lang.IllegalThreadStateException
at java.lang.Thread.start(Native Method)
at MainClass.startSchedulerTest(MainClass.java:31)
at TestThread.run(TestThread.java:33)
java.lang.IllegalThreadStateException
at java.lang.Thread.start(Native Method)
at MainClass.startSchedulerTest(MainClass.java:31)
at TestThread.run(TestThread.java:33)
java.lang.IllegalThreadStateException
at java.lang.Thread.start(Native Method)
at MainClass.startSchedulerTest(MainClass.java:31)
at TestThread.run(TestThread.java:33)
Woke up in ThreadController! Joining TestThreads...
Ending Program!

A:\>

Could someone please tell me what's going wrong and how should I correct it! :sad:

Thanks to all.

Hey, I've found a problem in your code that explains the errors you've been getting. It would appear that your test threads all try to execute sch.start(). Once sch.start() has been run any additional calls to it will result in the exception you got regardless of the state of the first thread that was created when you called sch.start for the first time.

For a demonstration try replacing your main with this one and you will see the same exception.

public static void main(String[] args){
try{

TestThread test = new TestThread(" Creating Thread ");
for(int i = 0;i<5;i++){

test.start();
Thread.sleep(3000);
}
}
catch(Exception e){
e.printStackTrace();
}
}


I'm not sure what your hoping to accomplish but you may need to restructure you code. Hope it helps

Thanks all!

I have found and fixed the problem.

Regards.

Could you please explain what the problem was? I'm having trouble with the exact same problem.

Hi,
The explaination goes like this... Once you have started the thread you cant start is again for the simple reason that the thread is already in start state ... So if you try to start a thread which is already in start state you will get IllegalThreadState Exception!!

This article has been dead for over six months. Start a new discussion instead.