Java Threading Learning (ii)

Source: Internet
Author: User

This is a know project total of three classes
The first one:
Package com;


Army Threads
Simulate the behavior of both sides of the battle
public class Armyrunnable implements Runnable {

Volatile ensures that threads can read the values written by other threads correctly
Visibility of
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 () + "attacking each other [" + i + "]");
Let's give the processor time. The next one, who's attacking?
Thread.yield ();
}
}
System.out.println (Thread.CurrentThread (). GetName () + "End battle!" ");
}



}
The second one:
Package com;
/*
* Sui and Tang kingdoms drama Stage
* */
public class Stage extends Thread {


public void Run () {
System.out.println ("Welcome to the Sui and Tang Kingdoms!") ");
try {
Thread.Sleep (5000);
} catch (Interruptedexception E1) {
TODO auto-generated Catch block
E1.printstacktrace ();
}
System.out.println ("The big curtain slowly pulls apart");
try {
Thread.Sleep (5000);
} catch (Interruptedexception E1) {
TODO auto-generated Catch block
E1.printstacktrace ();
}

System.out.println ("The words of the Sui Dynasty, the army and the uprising of the soldiers killed pitch darkness ....") ");
Armyrunnable armytaskofsuidynastry = new armyrunnable ();
Armyrunnable Armytaskofrevolt = new armyrunnable ();

Creating a thread using the Runnable interface
Thread armyofsuidynastry = new Thread (armytaskofsuidynastry, "Army");
Thread Armyofrevolt = new Thread (Armytaskofrevolt, "Uprising Army");

Start the thread and let the Army start fighting.
Armyofsuidynastry.start ();
Armyofrevolt.start ();


The stage thread sleeps and everyone is watching the army fight.
try {
Thread.Sleep (50);
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
System.out.println ("Just as the two sides are fighting, proving");
Thread Mrcheng = new Keypersonthread ();
Mrcheng.setname ("Cheng Bite Gold");
System.out.println ("Cheng Jin's ideal is to end the battle, is the people live in peace and contentment!" ");
Armytaskofsuidynastry.keeprunning = false;
Armytaskofrevolt.keeprunning = false;

try {
Thread.Sleep (2000);
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

/*
* Historical drama left to key people
* */
Mrcheng.start ();



Attention, all threads waiting for Mr. To complete the historical mission
try {
Mrcheng.join ();
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

System.out.println ("The end of the war, the people live in peace and contentment, Mr. Cheng realized a positive life dream, for the people to make a contribution");
System.out.println ("End of Performance, Goodbye");
}
public static void Main (string[] args) {
TODO auto-generated Method Stub
New Stage (). Start ();


}


}

The third one: Package com;


public class Keypersonthread extends Thread {

public void Run () {
System.out.println (Thread.CurrentThread (). GetName () + "Start fighting!" ");
for (int i = 0; i < ++i) {
System.out.println (Thread.CurrentThread (). GetName () + "left the right to kill, attack the army!" ");
}
System.out.println (Thread.CurrentThread (). GetName () + "The battle is over!" ");
}


}

Java Threading Learning (ii)

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.