Results 1 to 11 of 11
Thread: Thread RUNNABLE or WAITING
- 01-06-2010, 08:32 AM #1
Member
- Join Date
- Jan 2010
- Posts
- 4
- Rep Power
- 0
Thread RUNNABLE or WAITING
I am working on some thread problems . In one of the problems , I have to check the state of a thread. What I am doing is, I create a thread , assign one task to it and after starting it I pass this thread to a method ,which checks thread's state using thread.getState().Now the problem is, for one of the thread , in run() method, I created one object and called wait() method on it. Now for this thread some times thread.getState() returns WAITING and some times RUNNABLE. I am surprised that how a waiting thread can be in RUNNABLE state.
Here is the code snippet :
try {
final Object obj = "";
Thread t2 = new Thread(new Runnable() {
public void run() {
try {
synchronized (obj) {
obj.wait();
}
} catch (InterruptedException e) {
}
}
});
t2.start();
Here t2.getState() gives WAITING 9 out of 10 times but once it also gives RUNNABLE.
Please help me out from this problemLast edited by Pushkar; 01-06-2010 at 08:37 AM. Reason: want to add code snippet also.
- 01-06-2010, 08:56 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 14
It reaches the RUNNABLE state before it reaches the waiting state.
Sometimes the main thread is calling getState before the waiting state is reached.
- 01-06-2010, 11:05 AM #3
Member
- Join Date
- Jan 2010
- Posts
- 4
- Rep Power
- 0
Thanks
Thanks for quick reply. I totally agree with u, but the thing is how can i ensure that the waiting state.
- 01-06-2010, 11:11 AM #4
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 14
How can you ensure what?
What is the problem that you are trying to solve here?
Why are you unhappy with the thread getting into the RUNNABLE state first?
- 01-06-2010, 11:15 AM #5
Member
- Join Date
- Jan 2010
- Posts
- 4
- Rep Power
- 0
@r035198x
Sorry for incomplete response.
I want to ensure the waiting state of thread t2, because according to my problem structure, I have to interrupt a waiting thread .
So how can I prevent thread t2 to enter from waiting to runnable state.
- 01-06-2010, 11:30 AM #6
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 14
If you want to keep the thread waiting then don't call notify/notifyAll as you are already doing. If you want to do something only when t2 is in waiting state then set a flag when t2 starts waiting and continously check for that flag. All this smells bad.
What really is the object of this exercise?
- 01-06-2010, 11:40 AM #7
Member
- Join Date
- Jan 2010
- Posts
- 4
- Rep Power
- 0
@r035198x
Actually I m a student and I have been given these exercises as assignments. That is why I have posted this problem
- 01-06-2010, 11:44 AM #8
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 14
Why not post the exact problem description for this one.
- 01-13-2010, 01:07 AM #9
Just loop and check the state until it goes to waiting, with a short sleep() in the loop.
- 01-13-2010, 02:48 AM #10
Yea like Steve11235 mentioned, just sleep() for couple of seconds and then do getState()
"Experience is what you get when you don't get what you want" (Dan Stanford)
"Rise and rise again until lambs become lions" (Robin Hood)
- 01-14-2010, 02:36 AM #11
Similar Threads
-
runnable interface in thread
By ADARSH in forum New To JavaReplies: 1Last Post: 11-01-2009, 09:08 PM -
Once again: waiting in a thread loop.
By willemjav in forum Threads and SynchronizationReplies: 115Last Post: 09-22-2008, 02:35 PM -
waiting for a file
By Fleur in forum New To JavaReplies: 2Last Post: 06-23-2008, 09:18 PM -
Creating a Thread using the Runnable interface
By Java Tip in forum java.langReplies: 0Last Post: 04-09-2008, 07:31 PM
Bookmarks