Public class threadtest {
Public static void main (string [] ARGs) throws interruptedexception {
// Todo auto-generated method stub
New threadtest (). INIT ();
}
Public void Init () throws interruptedexception
{
Final business = new business ();
New thread (New runnable ()
{
Public void run (){
For (INT I = 0; I <50; I ++)
{
Business. subthread (I );
}
}
}
). Start ();
Thread. Sleep (1000); // This row is executed first by the subthread to let the main thread out of the CPU
For (INT I = 0; I <50; I ++)
{
Business. mainthread (I );
}
}
Class business
{
// Boolean bshouldsub = true;
Public synchronized void mainthread (int I) throws interruptedexception
{
For (Int J = 0; j <100; j ++)
{
System. Out. println (thread. currentthread (). getname () + ": yu_ I =" + I + ", j =" + J );
}
This. Policy ();
Try {
This. Wait ();
} Catch (interruptedexception e ){
E. printstacktrace ();
}
}
Public synchronized void subthread (int I)
{
For (Int J = 0; j <10; j ++)
{
System. Out. println (thread. currentthread (). getname () + ": bing_ I =" + I + ", j =" + J );
}
This. Policy ();
Try {
This. Wait ();
} Catch (interruptedexception e ){
E. printstacktrace ();
}
}
}
}