• Post Reply Bookmark Topic Watch Topic
  • New Topic

Working on an Object with two threads of another class.  RSS feed

 
Ramesh Kumar Swarnkar
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everybody,
I have a class A, that has two threads t1 and t2. These two threads try to access another class B want to do some operatoin available in its run() method. here is code :




While Executing class A; an exception occurs:
java.lang.IllegalThreadStateException

My aim is to run two separate thread of class A and do some processing in run() of class B.
Can any one please suggest, what is wrong in this code, how to avoid this exception ?

~Thanks
[ September 23, 2008: Message edited by: Ramesh Kumar Swarnkar ]
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its better to provide the entire stack trace rather than just giving the exception name.

Anyways,
It seems that the exception will come because you are starting the thread "th" in class B twice. Once each through t1 and t2 in class A.
You are not allowed to start any thread twice.

I assume you have to run two parallel threads that do some processing as done in B.startSearch()
If yes, then you do not need to start a new thread inside B.
Also, you need to be quite sure that B is thread safe if you are sharing the same instance between two threads i.e. t1 and t2 in A.

Also, it is useless for A to implement Runnable.
 
Sudarshan Chakrabarty
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Changing the run() of Class A as follows should solve your problem :

 
Sudarshan Chakrabarty
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically, what you were doing wrong in your example was that you were creating one thread in B and two threads in A and both your threads of A were trying to manipulate the single thread of B.
Now, the thread "t1" of Class A calls the startSearch() which in turn calls the run() on thread "th" in B. Once the run() is completed the thread "th" is dead(terminated) as per the figure below.



Now, when you call the startSearch() from "t2" of class A, it again uses the same thread object "th" from B and tries to call the start() on it. As this thread is already dead ( run() method terminated in the previous call), it is unable to start and hence throws an IllegalThreadStateException. Bottom line being, you can't start a dead thread.
 
Ramesh Kumar Swarnkar
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nitesh and Sudarshan,
Thank you for your response.

Seems my assumption was wrong, I was thinking as:
once the thread t1 goes into sleep state. thread th of class B finishes its execution and enters into dead state.
Hence, after sleep time thread t2 of class A will treat the thread th as a new thread and try to start it again. Also,I thought, it would be throwing exception if I would have called thread t1.start() and t2.start() without any interval of sleep time in between. Because may be thread th would be in running state when thread t2 tries to call it immendiately just after thread t1 call.

Anyway, I modified my code of Class A (re written by avoiding Runnable interface). The motto of code will be clear if you can give a look to it:




//Class B:



while running class A, it gives output as:
Inside run() of Class B
startSearch() is called for URL:http://www.a.com
startSearch() is called for URL:http://www.b.com
startSearch() is called for URL:http://www.a.com
URLs visited are :[null]

However, I was expecting as :
Inside run() of Class B
startSearch() is called for URL:http://www.a.com
Inside run() of Class B
startSearch() is called for URL:http://www.b.com
Inside run() of Class B
startSearch() is called for URL:http://www.a.com
URL already visited-http://www.a.com

URLs visited are :[http://www.a.com,http://www.b.com]

What is wrong here....seems I miss something here.....???!!!??
Does it is because run is not getting called on its own. How to get run() executed again and again....?
 
Ramesh Kumar Swarnkar
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends,
is this code legthy to understand ? hope not....please suggest me how to get my goal acomplished here.
[ September 24, 2008: Message edited by: Ramesh Kumar Swarnkar ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!