Java code
Public class TestThread {
Int count = 0;
Object lock = new Object ();
Transient boolean isWorking = true;
Thread t1 = new Thread (){
Public void run (){
While (true ){
Try {
If (count ++ % 10 = 0 ){
System. out. println ("sleeping ");
IsWorking = false;
Synchronized (lock ){
Lock. wait ();
}
IsWorking = true;
System. out. println ("Awake ");
} Else {
System. out. println ("Sleep" + count );
Thread. sleep (100 );
}
} Catch (Exception e ){
E. printStackTrace ();
}
}
}
};
Thread t2 = new Thread (){
Public void run (){
While (true ){
Try {
If (! IsWorking ){
System. out. println ("Wake up! ");
Synchronized (lock ){
Lock. Policy ();
}
}
Thread. sleep (2000 );
} Catch (Exception e ){
E. printStackTrace ();
}
}
}
};
Public TestThread (){
T1.start ();
T2.start ();
}
Public static void main (String [] args ){
New TestThread ();
}
}
Public class TestThread {
Int count = 0;
Object lock = new Object ();
Transient boolean isWorking = true;
Thread t1 = new Thread (){
Public void run (){
While (true ){
Try {
If (count ++ % 10 = 0 ){
System. out. println ("sleeping ");
IsWorking = false;
Synchronized (lock ){
Lock. wait ();
}
IsWorking = true;
System. out. println ("Awake ");
} Else {
System. out. println ("Sleep" + count );
Thread. sleep (100 );
}
} Catch (Exception e ){
E. printStackTrace ();
}
}
}
};
Thread t2 = new Thread (){
Public void run (){
While (true ){
Try {
If (! IsWorking ){
System. out. println ("Wake up! ");
Synchronized (lock ){
Lock. Policy ();
}
}
Thread. sleep (2000 );
} Catch (Exception e ){
E. printStackTrace ();
}
}
}
};
Public TestThread (){
T1.start ();
T2.start ();
}
Public static void main (String [] args ){
New TestThread ();
}
}