Threads
Threads
➢ Inside run( ), you will define the code that constitutes the new thread.
➢ run( ) establishes the entry point for another concurrent thread of
execution within your program.
➢ This thread will end when run( ) returns.
while(condition){
try{
wait();
}catch(InterruptedException e){}
}
Advanced OOP - Concurrency 40
7. Thread Communication
class TickTock{
String state = "Tick"; //initial state is Tick
synchronized void tick(){ //prints Tick
while(state.compareTo("Tock")== 0){ //wait while state is Tock
try{
wait();
}catch(InterruptedException e){}
}
System.out.print("Tick "); //print Tick
state = "Tock"; //set state to Tock
Notify(); //notify the other thread
}