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.