?? multithread.java
字號(hào):
package 操作系統(tǒng);
import java.io.*;
//多線程編程
public class MultiThread {
@SuppressWarnings("deprecation")
public static void main(String args[]) {
System.out.println("我是主線程!");
// 下面創(chuàng)建線程實(shí)例thread1
ThreadUseExtends thread1 = new ThreadUseExtends();
// 創(chuàng)建thread2時(shí)以實(shí)現(xiàn)了Runnable接口的THhreadUseRunnable類實(shí)例為參數(shù)
Thread thread2 = new Thread(new ThreadUseRunnable(), "SecondThread");
thread1.start();// 啟動(dòng)線程thread1使之處于就緒狀態(tài)
// thread1.setPriority(6);//設(shè)置thread1的優(yōu)先級(jí)為6
// 優(yōu)先級(jí)將決定cpu空出時(shí),處于就緒狀態(tài)的線程誰(shuí)先占領(lǐng)cpu開始運(yùn)行
// 優(yōu)先級(jí)范圍1到10,MIN_PRIORITY,MAX_PRIORITY,NORM_PAIORITY
// 新線程繼承創(chuàng)建她的父線程優(yōu)先級(jí),父線程通常有普通優(yōu)先級(jí)即5NORM_PRIORITY
System.out.println("主線程將掛起7秒!");
try {
Thread.sleep(7000);// 主線程掛起7秒
} catch (InterruptedException e) {
return;
}
System.out.println("又回到了主線程!");
if (thread1.isAlive()) {
thread1.stop();// 如果thread1還存在則殺掉他
System.out.println("thread1休眠過長(zhǎng),主線程殺掉了thread1!");
} else
System.out.println("主線程沒發(fā)現(xiàn)thread1,thread1已醒順序執(zhí)行結(jié)束了!");
thread2.start();// 啟動(dòng)thread2
System.out.println("主線程又將掛起7秒!");
try {
Thread.sleep(7000);// 主線程掛起7秒
} catch (InterruptedException e) {
return;
}
System.out.println("又回到了主線程!");
if (thread2.isAlive()) {
thread2.stop();// 如果thread2還存在則殺掉他
System.out.println("thread2休眠過長(zhǎng),主線程殺掉了thread2!");
} else
System.out.println("主線程沒發(fā)現(xiàn)thread2,thread2已醒順序執(zhí)行結(jié)束了!");
System.out.println("程序結(jié)束按任意鍵繼續(xù)!");
try {
System.in.read();
} catch (IOException e) {
System.out.println(e.toString());
}
}// main
}// MultiThread
class ThreadUseExtends extends Thread
// 通過繼承Thread類,并實(shí)現(xiàn)它的抽象方法run()
// 適當(dāng)時(shí)候創(chuàng)建這一Thread子類的實(shí)例來實(shí)現(xiàn)多線程機(jī)制
// 一個(gè)線程啟動(dòng)后(也即進(jìn)入就緒狀態(tài))一旦獲得CPU將自動(dòng)調(diào)用它的run()方法
{
ThreadUseExtends() {
}// 構(gòu)造函數(shù)
public void run() {
System.out.println("我是Thread子類的線程實(shí)例!");
System.out.println("我將掛起10秒!");
System.out.println("回到主線程,請(qǐng)稍等,剛才主線程掛起可能還沒醒過來!");
try {
sleep(10000);// 掛起5秒
} catch (InterruptedException e) {
return;
}
// 如果該run()方法順序執(zhí)行完了,線程將自動(dòng)結(jié)束,而不會(huì)被主線程殺掉
// 但如果休眠時(shí)間過長(zhǎng),則線程還存活,可能被stop()殺掉
}
}
class ThreadUseRunnable implements Runnable
// 通過實(shí)現(xiàn)Runnable接口中的run()方法,再以這個(gè)實(shí)現(xiàn)了run()方法的類
// 為參數(shù)創(chuàng)建Thread的線程實(shí)例
{
// Thread thread2=new Thread(this);
// 以這個(gè)實(shí)現(xiàn)了Runnable接口中run()方法的類為參數(shù)創(chuàng)建Thread類的線程實(shí)例
ThreadUseRunnable() {
}// 構(gòu)造函數(shù)
public void run() {
System.out.println("我是Thread類的線程實(shí)例并以實(shí)現(xiàn)了Runnable接口的類為參數(shù)!");
System.out.println("我將掛起1秒!");
System.out.println("回到主線程,請(qǐng)稍等,剛才主線程掛起可能還沒醒過來!");
try {
Thread.sleep(1000);// 掛起5秒
} catch (InterruptedException e) {
return;
}
// 如果該run()方法順序執(zhí)行完了,線程將自動(dòng)結(jié)束,而不會(huì)被主線程殺掉
// 但如果休眠時(shí)間過長(zhǎng),則線程還存活,可能被stop()殺掉
}
}
// 該程序可做的修改如改休眠時(shí)間或優(yōu)先級(jí)setPriority()
/*
* Thread(); //建立一個(gè)線程void run(); //對(duì)于一個(gè)繼承了Runnable接口的class而言,他運(yùn)行一個(gè)線程,否著他什么都不做
* void setPriority(int newPriority); //設(shè)置優(yōu)先級(jí)void start(); //運(yùn)行一個(gè)程序void
* sleep(long millis); //線程睡眠millis毫秒static void yield(); //臨時(shí)pause一個(gè)程序以便起他線程運(yùn)行
*/
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -