Home » Java » JAVA multi-threaded a simple case

JAVA multi-threaded a simple case

JAVA multi-threaded a simple case, there is a need for friends, you can refer to.


has only four classes, the first three to describe the sleep, wake, and end of the thread, and the last class is a small, isolated example, looking at the last, looking at the

in front


package com.imooc.concurrent.base;
/ * *
* drama stage of Sui and Tang Dynasties
* /
Public, class, Stage, extends, Thread {
Public, void, run () {
System.out.println (welcome to the Sui and Tang Dynasties);
"Let the audience to be quiet for a moment, waiting for the drama staged
{try
Thread.sleep (5000);
} catch (InterruptedException, E1) {
E1.printStackTrace ();
}
System.out.println ("big curtain slowly opens");
{try
Thread.sleep (5000);
} catch (InterruptedException, E1) {
E1.printStackTrace ();
}
System.out.println ("the words of the Sui Dynasty, the Sui Dynasty and the peasant uprising army to kill pitch-dark...");
ArmyRunnable armyTaskOfSuiDynasty = new, ArmyRunnable ();
ArmyRunnable armyTaskOfRevolt = new, ArmyRunnable ();
/ / create a thread using the Runnable interface
Thread armyOfSuiDynasty = new Thread (armyTaskOfSuiDynasty, Sui Jun);
Thread armyOfRevolt = new Thread (armyTaskOfRevolt, peasant uprising army);
/ / start a thread, let the army began to fight
ArmyOfSuiDynasty.start ();
ArmyOfRevolt.start ();
The stage / thread to sleep, we concentrate on watching the fight
{try
Thread.sleep (50);
} catch (InterruptedException, e) {
E.printStackTrace ();
}
System.out.println ("as both sides in full swing, grab the middle);
Thread mrCheng = new, KeyPersonThread ();
MrCheng.setName ("Cheng Yaojin");
System.out.println (Cheng Yaojin's ideal is to end the war and make the people live and work in peace and contentment.);
/ / stop military operations
Stop / / thread
ArmyTaskOfSuiDynasty.keepRunning = false;
ArmyTaskOfRevolt.keepRunning = false;
{try
Thread.sleep (2000);
} catch (InterruptedException, e) {
E.printStackTrace ();
}
*
* historical dramas are reserved for key players
* /
MrCheng.start ();
/ / anticipated, all threads waiting for Mr. Cheng to accomplish the historical mission
{try
MrCheng.join ();
} catch (InterruptedException, e) {
E.printStackTrace ();
}
System.out.println ("the end of the war, the people live and work in peace and contentment), Mr. Cheng has achieved a positive life dream, and made contributions to the people!";
System.out.println (thanks for watching the Sui and Tang Dynasties, see you later);
}
Public, static, void, main (String[], args) {
New Stage ().Start ();
}
}




package com.imooc.concurrent.base;
/ / Army thread
The behavior of both / combat simulation
Public, class, ArmyRunnable, implements, Runnable {
//volatile guarantees that threads can correctly read values written by other threads
Ref JMM happens-before / / visibility, principle
Volatile, Boolean, keepRunning = true;
@Override
Public, void, run () {
While (keepRunning) {
5 / launch Combo
For (int i=0; i<5; i++) {
System.out.println (Thread.currentThread ().GetName () + "attack each other [+i+] []];
A / / processor time, the next time, who is not necessarily offensive!
Thread.yield ();
}
}
System.out.println (Thread.currentThread ().GetName () + "ended the fight!";
}
}





package com.imooc.concurrent.base;
Public, class, KeyPersonThread, extends, Thread {
Public, void, run () {
System.out.println (Thread.currentThread ().GetName () + begins combat;
For (int i=0; i<10; i++) {
System.out.println (Thread.currentThread ().GetName () + "left, right, right, kill, attack, Sui Jun...";
}
System.out.println (Thread.currentThread ().GetName () + "ended the fight!";
}
}


suggests starting with the following one to see.


package com.imooc.concurrent;
Public, class, Actor, extends, Thread {
Public, void, run () {
System.out.println (getName () + "is an actor!");
Int count = 0;
Boolean keepRunning = true;
While (keepRunning) {
System.out.println (getName () + "staging:" + (++count);
If (count = = 100) {
KeepRunning = false;
}
If (count%10== 0) {
{try
Thread.sleep (1000);
} catch (InterruptedException, e) {
E.printStackTrace ();
}
}
}
The System.out.println (getName) + show is over;
}
Public, static, void, main (String[], args) {
Thread actor = new, Actor ();
Actor.setName ("Mr. Thread");
Actor.start ();
Thread, actressThread = new, Thread (new, Actress (), Ms., Runnable);
ActressThread.start ();
}
}
Class Actress implements Runnable{
@Override
Public, void, run () {
System.out.println (Thread.currentThread ().GetName () + is an actor;
Int count = 0;
Boolean keepRunning = true;
While (keepRunning) {
System.out.println (Thread.currentThread ().GetName () + "staging:" + (++count);
If (count = = 100) {
KeepRunning = false;
}
If (count%10== 0) {
{try
Thread.sleep (1000);
} catch (InterruptedException, e) {
E.printStackTrace ();
}
}
}
The System.out.println (Thread.currentThread).GetName (+) + is finished;
}
}





Latest