Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old August 19th, 2004, 01:06 AM
pentagenius pentagenius is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 3 pentagenius User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 46 sec
Reputation Power: 0
Question IllegalThreadState Problem!

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:
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!

Thanks to all.

Reply With Quote
  #2  
Old August 19th, 2004, 05:02 PM
smitam smitam is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 2 smitam User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
  #3  
Old August 20th, 2004, 01:01 AM
pentagenius pentagenius is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 3 pentagenius User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 46 sec
Reputation Power: 0
Quote:
Originally Posted by smitam
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
Thanks smitam for your reply!

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?

Reply With Quote
  #4  
Old August 20th, 2004, 09:34 AM
pentagenius pentagenius is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 3 pentagenius User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 46 sec
Reputation Power: 0
Thanks all!

I have found and fixed the problem.

Regards.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > IllegalThreadState Problem!


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT