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)