JAVA線程學習(二),java線程學習

來源:互聯網
上載者:User

JAVA線程學習(二),java線程學習
這是一個曉得項目共有三個類
第一個:
package com;


//軍隊線程
//類比作戰雙方的行為
public class ArmyRunnable implements Runnable {

//volatile保證了線程可以正確的讀取其他線程寫入的值
//可見度
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()+ "進攻對方["+ i +"]");
//讓出了處理器的時間下一次該誰進攻還不一定呢
Thread.yield();
}
}
System.out.println(Thread.currentThread().getName()+ "結束戰鬥!");
}



}
第二個:
package com;
/*
 * 隋唐演義大戲舞台
 * */
public class Stage extends Thread {


public void run(){
System.out.println("歡迎觀看隋唐演義!");
try {
Thread.sleep(5000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("大幕徐徐拉開");
try {
Thread.sleep(5000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

System.out.println("話說隋朝末年,隨軍與農民起義軍殺的昏天黑地。。。。");
ArmyRunnable armyTaskOfSuiDynastry = new ArmyRunnable();
ArmyRunnable armyTaskOfRevolt = new ArmyRunnable();

//使用Runnable介面建立線程
Thread armyOfSuiDynastry = new Thread(armyTaskOfSuiDynastry, "隨軍");
Thread armyOfRevolt = new Thread(armyTaskOfRevolt, "農民起義軍");

//啟動線程讓軍隊開始作戰
armyOfSuiDynastry.start();
armyOfRevolt.start();


//舞台線程休眠,大家專心觀看軍隊的廝殺
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("正當雙方激戰正酣, 半路殺出個程咬金");
Thread mrCheng = new KeyPersonThread();
mrCheng.setName("程咬金");
System.out.println("程咬金的理想就是結束戰鬥, 是百姓安居樂業!");
armyTaskOfSuiDynastry.keepRunning = false;
armyTaskOfRevolt.keepRunning = false;

try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

/*
* 曆史大戲留給關鍵人物
* */
mrCheng.start();



//萬眾矚目,所有線程等待先生完成曆史使命
try {
mrCheng.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

System.out.println("戰爭結束,人民安居樂業,程先生實現了積極地人生夢想,為人民做出了貢獻");
System.out.println("表演結束,再見");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Stage().start();


}


}

第三個:package com;


public class KeyPersonThread 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() + "戰鬥結束了!");
}


}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.