JAVA thread Learning (2), java thread Learning
This is a project with three classes.
First:
Package com;
// Military thread
// Simulate actions of both sides
Public class ArmyRunnable implements Runnable {
// Volatile ensures that the thread can correctly read the value written by other threads
// Visibility
Volatile boolean keepRunning = true;
@ Override
Public void run (){
// TODO Auto-generated method stub
While (keepRunning)
{
For (int I = 0; I <5; ++ I)
{
System. out. println (Thread. currentThread (). getName () + "attack the other party [" + I + "]");
// Who should attack the next time given the processor's time?
Thread. yield ();
}
}
System. out. println (Thread. currentThread (). getName () + "End the battle! ");
}
}
Second:
Package com;
/*
* Stage
**/
Public class Stage extends Thread {
Public void run (){
System. out. println ("welcome to the Sui and Tang Dynasties! ");
Try {
Thread. sleep (5000 );
} Catch (InterruptedException e1 ){
// TODO Auto-generated catch block
E1.printStackTrace ();
}
System. out. println ("curtain opened slowly ");
Try {
Thread. sleep (5000 );
} Catch (InterruptedException e1 ){
// TODO Auto-generated catch block
E1.printStackTrace ();
}
System. out. println ("the last year of the Sui Dynasty, when the army and the peasants joined forces to kill the army, it was dark .... ");
ArmyRunnable armyTaskOfSuiDynastry = new ArmyRunnable ();
ArmyRunnable armyTaskOfRevolt = new ArmyRunnable ();
// Use the Runnable interface to create a thread
Thread armyOfSuiDynastry = new Thread (armyTaskOfSuiDynastry, "suijun ");
Thread armyfrevolt = new Thread (armyTaskOfRevolt, "");
// Start the thread to start the army
ArmyOfSuiDynastry. start ();
Armyfrevolt. start ();
// The stage thread is sleeping, so you can watch the army fight
Try {
Thread. sleep (50 );
} Catch (InterruptedException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
System. out. println ");
Thread mrCheng = new KeyPersonThread ();
MrCheng. setName ("Cheng Zhijin ");
System. out. println ("Cheng Zhijin's ideal is to end the battle. It is the people who live and live! ");
ArmyTaskOfSuiDynastry. keepRunning = false;
ArmyTaskOfRevolt. keepRunning = false;
Try {
Thread. sleep (2000 );
} Catch (InterruptedException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
/*
* Historical events are reserved for key figures
**/
MrCheng. start ();
// All threads are waiting for Mr to fulfill his historical mission
Try {
MrCheng. join ();
} Catch (InterruptedException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
System. out. println ("when the war is over and the people live and work in peace, Mr. Cheng has realized his dream of active life and contributed to the people ");
System. out. println ("the performance is over, goodbye ");
}
Public static void main (String [] args ){
// TODO Auto-generated method stub
New Stage (). start ();
}
}
Third:Package com;
Public class KeyPersonThread extends Thread {
Public void run (){
System. out. println (Thread. currentThread (). getName () + "started fighting! ");
For (int I = 0; I <10; ++ I ){
System. out. println (Thread. currentThread (). getName () + "left-right kill, attack with the army! ");
}
System. out. println (Thread. currentThread (). getName () + "the battle is over! ");
}
}