tips.threads
Class NotifyApp

java.lang.Object
  extended bytips.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).


Nested Class Summary
(package private) static class NotifyApp.Rendezvous
          Inner Class...
(package private) static class NotifyApp.Waiter
          Inner class -
 
Field Summary
(package private) static int g_nRootNum
           
(package private) static int MAX_THREADS
           
(package private) static NotifyApp.Rendezvous rendezvous
           
 
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
 

Field Detail

MAX_THREADS

static final int MAX_THREADS
See Also:
Constant Field Values

g_nRootNum

static int g_nRootNum

rendezvous

static NotifyApp.Rendezvous rendezvous
Constructor Detail

NotifyApp

public NotifyApp()
Creates a new instance of NotifyApp

Method Detail

main

public static void main(java.lang.String[] args)