This discussion is archived
1 2 Previous Next 24 Replies Latest reply: Feb 17, 2013 12:47 AM by EJP RSS

Can a Thread wake up itself?

801554 Newbie
Currently Being Moderated
I know a Thread can wake another that blocked on obj.wait() by calling obj.notify(), but is it possible that a Thread can wake itself--with obj.notify() followed by obj.wait()?
Below is my test code:
public class VisibilityTest {
     public static volatile int i = 0;
     private static Object lock = new Object();

     public static void main(String args[]) throws InterruptedException {
          Thread trd1 = new Thread(new Runnable() {
               public void run() {
                    while (true) {
                         ++i; //it is not atomic, but no other thread access i at this time.
                         synchronized(lock) {
                              lock.notify();
                              try {
                                   //Thread.sleep(1); //try to avoid notify itself--it do happens!!!
                                   lock.wait();
                              } catch (InterruptedException e) {
                                   e.printStackTrace();
                              }
                         }
                    }
               }
          });
          
          Thread trd2 = new Thread(new Runnable() {
               private int j = 0;
               public void run() {
                    while (true) {
                         int k = i - j;
                         if (k != 1) {
                              System.out.println("I see you : j="+j + "; k=" + k);
                         }
                         j = i;
                         
                         synchronized(lock) {
                              lock.notify();
                              try {
                                   //Thread.sleep(1); //try to avoid notify itself--it do happens!!!
                                   lock.wait();
                              } catch (InterruptedException e) {
                                   e.printStackTrace();
                              }
                         }
                    }
               }
          });
          
          trd1.start();
          Thread.sleep(10);
          trd2.start();
     }
}
If I run the code above on my eclipse(with jdk1.6), I see output like this:

I see you : j=1467887; k=0
I see you : j=1540880; k=0
I see you : j=1547851; k=0
I see you : j=1617674; k=0
I see you : j=1704345; k=0
I see you : j=1730039; k=2
I see you : j=1743409; k=0
I see you : j=1803817; k=0
I see you : j=1996206; k=0
I see you : j=2097955; k=0
I see you : j=2242856; k=2
I see you : j=2295622; k=0
I see you : j=2385336; k=0

k=0 is not always more than k=2. It is easier to see this kind of output if you run the code in two process at the same time. The output is weird, I thought there should be no output. My explanation is trd1/trd2 wake up itself sometimes, but I can not accept this idea. Can anyone give a clue?

Edited by: teasp on 2013-2-13 上午5:51
  • 1. Re: Can a Thread wake itself?
    Kayaman Guru
    Currently Being Moderated
    Can you wake yourself up when you're sleeping?

    Also I don't know what you're trying to prove with your code riddled with errors, but calling notify() before wait() has no effect, and certainly no "thread self waking" effects.
  • 2. Re: Can a Thread wake itself?
    801554 Newbie
    Currently Being Moderated
    If you run the code on your machine and read it carefully, you will understand why notify() follows a wait().
  • 3. Re: Can a Thread wake itself?
    Kayaman Guru
    Currently Being Moderated
    teasp wrote:
    If you run the code on your machine and read it carefully, you will understand why notify() follows a wait().
    I know why notify() follows a wait().

    You were talking about wait() following a notify() causing a Thread to wake itself up, which is not correct.
  • 4. Re: Can a Thread wake itself?
    jtahlborn Expert
    Currently Being Moderated
    As you can read in the javadocs for wait/notify, these method calls don't make sense in and of themselves. you need some sort of condition test along with the notification, otherwise the purpose behind the notification gets lost.
  • 5. Re: Can a Thread wake up itself?
    EJP Guru
    Currently Being Moderated
    A thread that needs waking up must be asleep, so it isn't executing any code, so it can't execute any code that would wake it up. Your question doesn't make sense.
  • 6. Re: Can a Thread wake up itself?
    801554 Newbie
    Currently Being Moderated
    My wrong expression lead confusion. I should not use "wake up", what I mean is Can a Thread notify itself?. As show in the test code, when notify method follows wait method immediately, is there any chance that notify "make effect" on the later wait in the same Thread? The test result shows it is possible.
  • 7. Re: Can a Thread wake up itself?
    EJP Guru
    Currently Being Moderated
    Notify notifies threads that are currently waiting, nothing else. It doesn't post an event for later processing.
  • 8. Re: Can a Thread wake up itself?
    801554 Newbie
    Currently Being Moderated
    Yes, that is what in theory, I believed it too. But how to explain the weird output of the code, would you please take a look at it and give me a clue? Thanks a lot!
  • 9. Re: Can a Thread wake up itself?
    Kayaman Guru
    Currently Being Moderated
    teasp wrote:
    Yes, that is what in theory, I believed it too.
    It's not a theory to believe in.
    But how to explain the weird output of the code, would you please take a look at it and give me a clue?
    Your test is flawed so you're seeing gibberish results. I would rather not go through that code though, it looks horrible.
  • 10. Re: Can a Thread wake up itself?
    EJP Guru
    Currently Being Moderated
    Explain yourself. What exactly is weird about the output of the code? and where is the evidence that either of the threads is notifying itself? Your first comment on the i++ line is mistaken, for a start.
  • 11. Re: Can a Thread wake up itself?
    801554 Newbie
    Currently Being Moderated
    OK, let me explain why I suspect notify wait. There are only 2 Threads (please ignore the main Thread) in this code: trd1 and trd2. trd1's work is to increment i and trd2's work is to read i, but this 2 Threads can not be working at the same time, since when one is working the other must be in waiting state(If they can not notify themself). So I said, althogh ++i is not atomic behavior, no other Thread will access i when ++i is executing, it is still safe. Since i is volatile, trd2 should always get the latest value(after every ++i), and j in trd2 stores the i value before the latest ++i, then k should always be 1.

    There are two points deserve suspicion: one is the visibility of i, the other is "notify oneself". If the problem caused by visibility of i, k=2 should always follows k=0, and the amount of k=2 should be less than (or equal to) that of k=0. But the output does not like this, appearance of k=2 can be much more than k=0, or vice verse. If "notify oneself" is true, the output is reasonable. That is why I suspect "notify oneself".

    BTW, if I open the 2 commented out sleep(1), no output appears anymore--that is the correct result I want to see.

    Edited by: teasp on 2013-2-14 上午8:46

    Edited by: teasp on 2013-2-14 上午9:28
  • 12. Re: Can a Thread wake up itself?
    801554 Newbie
    Currently Being Moderated
    OK, you do not need to go through the code, but please point out one flawed part you have seen, just as EJP did. Thank you very much.
  • 13. Re: Can a Thread wake up itself?
    jtahlborn Expert
    Currently Being Moderated
    teasp wrote:
    OK, you do not need to go through the code, but please point out one flawed part you have seen, just as EJP did. Thank you very much.
    the point of this thread is still unclear to me? are you trying to deal with a bug in your code? just hypothesizing for the sake of hypothesizing? ... something else?
  • 14. Re: Can a Thread wake up itself?
    EJP Guru
    Currently Being Moderated
    this 2 Threads can not be working at the same time
    That's not true. The first thread can be incrementing 'i' while the second one is running.
    So I said, althogh ++i is not atomic behavior, no other Thread will access i when ++i is executing, it is still safe
    That's not true either. The only part of the code in either thread that is thread-safe is the part inside the syncrhonized block.

    I don't need to go any further. You already have two false assumptions.
1 2 Previous Next

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points