Results 1 to 11 of 11
  1. #1
    Pushkar is offline Member
    Join Date
    Jan 2010
    Posts
    4
    Rep Power
    0

    Red face 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 problem
    Last edited by Pushkar; 01-06-2010 at 08:37 AM. Reason: want to add code snippet also.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    It reaches the RUNNABLE state before it reaches the waiting state.

    Sometimes the main thread is calling getState before the waiting state is reached.

  3. #3
    Pushkar is offline Member
    Join Date
    Jan 2010
    Posts
    4
    Rep Power
    0

    Default Thanks

    Thanks for quick reply. I totally agree with u, but the thing is how can i ensure that the waiting state.

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    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?

  5. #5
    Pushkar is offline Member
    Join Date
    Jan 2010
    Posts
    4
    Rep Power
    0

    Default @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.

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    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?

  7. #7
    Pushkar is offline Member
    Join Date
    Jan 2010
    Posts
    4
    Rep Power
    0

    Default @r035198x

    Actually I m a student and I have been given these exercises as assignments. That is why I have posted this problem

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Why not post the exact problem description for this one.

  9. #9
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    Just loop and check the state until it goes to waiting, with a short sleep() in the loop.

  10. #10
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    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)

  11. #11
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

Similar Threads

  1. runnable interface in thread
    By ADARSH in forum New To Java
    Replies: 1
    Last Post: 11-01-2009, 09:08 PM
  2. Once again: waiting in a thread loop.
    By willemjav in forum Threads and Synchronization
    Replies: 115
    Last Post: 09-22-2008, 02:35 PM
  3. waiting for a file
    By Fleur in forum New To Java
    Replies: 2
    Last Post: 06-23-2008, 09:18 PM
  4. Creating a Thread using the Runnable interface
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-09-2008, 07:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •