tips.threads
Class NotifyApp
java.lang.Object
tips.threads.NotifyApp
- class NotifyApp
- extends java.lang.Object
Sample class demonstrating the use of notifyAll() on a monitor (any object
that uses the synchronized keyword to create a locking section of code).
The idea is simple, create one monitor object (inner class Rendezvous) and then
10 threads that each call the Rendezvous function hurryUpAndWait(). hurryUpAndWait()
does nothing but wait. When the last thread has been created and all ten are
are "waiting" then notifyAll are called and the thread objects print out the
order in which they're notified.
Sample output:
Create thread no: 0
Create thread no: 1
Create thread no: 2
Create thread no: 3
Create thread no: 4
Create thread no: 5
Create thread no: 6
Create thread no: 7
Create thread no: 8
Create thread no: 9
Starting the HeadWaiter
Main thread end
Notifying All...
Thread 0 just got notified
Thread 1 just got notified
Thread 2 just got notified
Thread 3 just got notified
Thread 4 just got notified
Thread 5 just got notified
Thread 6 just got notified
Thread 7 just got notified
Thread 8 just got notified
HeadWaiter Start
Head Waiter Notifying All...
Thread 9 just got notified
HeadWaiter end
The sample output demonstrates that the thread get notified in any order
and may not get notified at all (hence the need for the HeadWaiter class).
|
Constructor Summary |
NotifyApp()
Creates a new instance of NotifyApp |
|
Method Summary |
static void |
main(java.lang.String[] args)
|
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
MAX_THREADS
static final int MAX_THREADS
- See Also:
- Constant Field Values
g_nRootNum
static int g_nRootNum
rendezvous
static NotifyApp.Rendezvous rendezvous
NotifyApp
public NotifyApp()
- Creates a new instance of NotifyApp
main
public static void main(java.lang.String[] args)