利用《倚天屠龍記》中光明頂之戰來完成線程的創建及線程的基本操作
- GuangMingDing.java:舞臺線程
- HeroThread.java:六大門派線程
- SchoolRunnable.java:英雄線程(就是張無忌啦)
Runnable和Thread
@FunctionalInterface
public interface RunnableRunnable接口應由任何類實現,其實例將由線程執行。 該類必須定義一個無參數的方法,稱爲run 。
該接口旨在爲希望在活動時執行代碼的對象提供一個通用協議。 例如, Runnable由Thread類Thread 。 活躍的只是意味着一個線程已經啓動,還沒有被停止。
另外, Runnable提供了一個類被激活而不是Thread Thread類化的Thread 。 一個實現類Runnable可以在不繼承運行Thread實例化一個Thread實例,並在傳遞本身作爲目標。 在大多數情況下, Runnable接口應使用,如果你只打算重寫run()方法並沒有其他Thread方法。 這是重要的,因爲類不應該被子類化,除非程序員打算修改或增強類的基本行爲。
//門派線程
//模擬作戰雙方的行爲
public class SchoolRunnable implements Runnable {
//volatile保證了線程可以正確的讀取其他線程寫入的值
//可見性 ref JMM, happens-before原則
volatile boolean keepRunning = true;
@Override
public void run() {
while(keepRunning){
//發動5連擊
for(int i=0;i<5;i++){
System.out.println(Thread.currentThread().getName()+"進攻對方["+i+"]");
//讓出了處理器時間,下次該誰進攻還不一定呢!
Thread.yield();
}
}
System.out.println(Thread.currentThread().getName()+"結束了戰鬥!");
}
//英雄線程
public class HeroThread extends Thread {
public void run(){
System.out.println(Thread.currentThread().getName()+"開始了戰鬥!");
for(int i=0;i<10;i++){
System.out.println(Thread.currentThread().getName()+"打敗六大門派。。。");
}
System.out.println(Thread.currentThread().getName()+"結束了戰鬥!");
}
}
//舞臺(光明頂)線程
/**
* 光明頂激戰大戲
*/
public class GuangMingDing extends Thread {
public void run(){
System.out.println("光明頂之戰馬上開始");
//讓觀衆們安靜片刻,等待大戲上演
try {
Thread.sleep(5000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
System.out.println("大幕徐徐拉開");
try {
Thread.sleep(5000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
System.out.println("六大門派圍攻光明頂...");
SchoolRunnable MingSchool = new SchoolRunnable();
SchoolRunnable SixSchool= new SchoolRunnable();
//使用Runnable接口創建線程
Thread MingSchoolThread = new Thread(MingSchool,"明教");
Thread SixSchoolThread= new Thread(SixSchool,"六大門派");
//啓動線程,讓武林人士開始作戰
MingSchoolThread.start();
SixSchoolThread.start();
//舞臺線程休眠,大家專心觀看軍隊廝殺
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("正當雙方激戰正酣,張無忌閃亮登場。。。");
Thread mrZhang = new HeroThread();
mrZhang.setName("張無忌");
System.out.println("張無忌的理想就是天下太平,跟趙敏比翼雙飛!");
//停止作戰
//停止線程的方法
MingSchool.keepRunning = false;
SixSchool.keepRunning = false;
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
/*
* 歷史大戲留給關鍵人物
*/
mrZhang.start();
//萬衆矚目,所有線程等待程先生完成歷史使命
try {
mrZhang.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("光明頂之戰結束,張無忌成了教主!");
System.out.println("謝謝觀看血戰光明頂,再見!");
}
public static void main(String[] args) {
new GuangMingDing().start();
}
}
輸出樣例
光明頂之戰馬上開始
大幕徐徐拉開
六大門派圍攻光明頂…
明教進攻對方[0]
明教進攻對方[1]
六大門派進攻對方[0]
六大門派進攻對方[1]
六大門派進攻對方[2]
明教進攻對方[2]
明教進攻對方[3]
明教進攻對方[4]
明教進攻對方[0]
六大門派進攻對方[3]
六大門派進攻對方[4]
明教進攻對方[1]
明教進攻對方[2]
……
……
明教進攻對方[1]
明教進攻對方[2]
明教進攻對方[3]
正當雙方激戰正酣,張無忌閃亮登場。。。
明教進攻對方[4]
明教進攻對方[0]
明教進攻對方[1]
明教進攻對方[2]
明教進攻對方[3]
明教進攻對方[4]
六大門派進攻對方[1]
明教進攻對方[0]
明教進攻對方[1]
張無忌的理想就是天下太平,跟趙敏比翼雙飛!
六大門派進攻對方[2]
六大門派進攻對方[3]
明教進攻對方[2]
六大門派進攻對方[4]
六大門派結束了戰鬥!
明教進攻對方[3]
明教進攻對方[4]
明教結束了戰鬥!
張無忌開始了戰鬥!
張無忌打敗六大門派。。。
張無忌打敗六大門派。。。
張無忌打敗六大門派。。。
張無忌打敗六大門派。。。
張無忌打敗六大門派。。。
張無忌打敗六大門派。。。
張無忌打敗六大門派。。。
張無忌打敗六大門派。。。
張無忌打敗六大門派。。。
張無忌打敗六大門派。。。
張無忌結束了戰鬥!
光明頂之戰結束,張無忌成了教主!
謝謝觀看血戰光明頂,再見!