|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hi all,
I am having a problem with threads as demonstrated below: I have four very small and simple files. They are as follows: Code:
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: Quote:
Could someone please tell me what's going wrong and how should I correct it! Thanks to all. |
|
#2
|
|||
|
|||
|
Hi
I think you can call start() method only once for a thread. else if you try calling it more than once, an illegal ThreadStateException will be thrown And your SchedulerTest instance sch is a class variable for MainClass but each instance of TestThread in the array calls start on it when they call startSchedulerTest method. This could be a possible cause for your problem Thanks |
|
#3
|
|||
|
|||
|
Quote:
But in that case, could you advise how should I call the SchedulerTest thread from the TestThreads? I need a mechanism that after each TestThread has done some operations it should invoke the SchedulerTest to do something else. However, it cannot have a direct reference of SchedulerTest thread. Could you please suggest something? |
|
#4
|
|||
|
|||
|
Thanks all!
I have found and fixed the problem. Regards. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > IllegalThreadState Problem! |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|