English 中文(简体)
Understanding wait()
原标题:

I created this silly program to play with wait()

public class WaitTest {
    public static void main(String [] args) {

      System.out.print("1 ");
      synchronized(args){

        System.out.print("2 ");

        try {
          args.wait();
          args.notifyAll();
        }
        catch(InterruptedException e){ System.out.print("exception caught");}

        System.out.print("3 ");
      }
   }
}

On my machine, the code never gets to print 3, unless I write wait(100) or another number of milliseconds. Why is this?

最佳回答

You are doing the wait() before the notifyAll(). wait() is going to block. When you put the timeout value in, wait() will timeout and then your program will continue. If you want your program to work, create a thread and do your notifyAll() there. wait() and notifyAll are designed for interthread synchronization.

问题回答

wait and notifyAll are for multithreading. args.wait() will wait forever until some other thread calls args.notifyAll() or args.notify().

When you call args.wait(100), it is waiting for 100ms, timing out, and continuing.

If you are familiar with semaphores, that is basically what wait/notify are.

You only have one thread. wait() is waiting for a notify from another thread.

Since no other thread notifies the object s monitor you are waiting on, it is just blocking there. And since you are synchronizing and waiting on a local variable, hardly any other thread would be able to call notify() on it.

From http://java.sun.com/docs/books/tutorial/essential/concurrency/guardmeth.html

When wait is invoked, the thread releases the lock and suspends execution. At some future time, another thread will acquire the same lock and invoke Object.notifyAll, informing all threads waiting on that lock that something important has happened:

The Java Tutorial is an excellent learning resource.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签