For example, if the Sub-thread runs for 10 times and the main thread runs for 100 times, how to design
Public class threadcommunication {
Public static void main (string [] ARGs ){
Final output = new output ();
// Subthread
New thread (
New runnable (){
Public void run (){
For (INT I = 1; I <= 50; I ++ ){
Output. sub (I );
}
}
}). Start ();
// Main thread
For (INT I = 1; I <= 50; I ++ ){
Output. Main (I );
}
}
}
Class output {
Private Boolean flag = true; // thread status
Public synchronized void sub (int I ){
While (! Flag ){
Try {
This. Wait ();
} Catch (interruptedexception e ){
E. printstacktrace ();
}
}
For (Int J = 1; j <= 10; j ++ ){
System. Out. println ("+ J in the subthread" + I + "times );
}
Flag = false;
This. Y (); // wake up the thread
}
Public synchronized void main (int I ){
While (FLAG ){
Try {
This. Wait ();
} Catch (interruptedexception e ){
E. printstacktrace ();
}
}
For (Int J = 1; j <= 100; j ++ ){
System. Out. println ("main thread number" + I + "times in" + J );
}
Flag = true;
This. Policy ();
}