JAVA thread Learning (2), java thread Learning

Source: Internet
Author: User

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! ");
}


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.