下表列出了Thread類的一些重要方法:
序號 | 方法描述 |
---|---|
1 | public void start() 使該線程開始執(zhí)行;Java 虛擬機調用該線程的 run 方法。 |
2 | public void run() 如果該線程是使用獨立的 Runnable 運行對象構造的,則調用該 Runnable 對象的 run 方法;否則,該方法不執(zhí)行任何操作并返回。 |
3 | public final void setName(String name) 改變線程名稱,使之與參數(shù) name 相同。 |
4 | public final void setPriority(int priority) 更改線程的優(yōu)先級。 |
5 | public final void setDaemon(boolean on) 將該線程標記為守護線程或用戶線程。 |
6 | public final void join(long millisec) 等待該線程終止的時間最長為 millis 毫秒。 |
7 | public void interrupt() 中斷線程。 |
8 | public final boolean isAlive() 測試線程是否處于活動狀態(tài)。 |
測試線程是否處于活動狀態(tài)。 上述方法是被Thread對象調用的。下面的方法是Thread類的靜態(tài)方法。
序號 | 方法描述 |
---|---|
1 | public static void yield() 暫停當前正在執(zhí)行的線程對象,并執(zhí)行其他線程。 |
2 | public static void sleep(long millisec) 在指定的毫秒數(shù)內(nèi)讓當前正在執(zhí)行的線程休眠(暫停執(zhí)行),此操作受到系統(tǒng)計時器和調度程序精度和準確性的影響。 |
3 | public static boolean holdsLock(Object x) 當且僅當當前線程在指定的對象上保持監(jiān)視器鎖時,才返回 true。 |
4 | public static Thread currentThread() 返回對當前正在執(zhí)行的線程對象的引用。 |
5 | public static void dumpStack() 將當前線程的堆棧跟蹤打印至標準錯誤流。 |
如下的ThreadClassDemo 程序演示了Thread類的一些方法:
// 文件名 : DisplayMessage.java // 通過實現(xiàn) Runnable 接口創(chuàng)建線程 public class DisplayMessage implements Runnable { private String message; public DisplayMessage(String message) { this.message = message; } public void run() { while(true) { System.out.println(message); } } }
// 文件名 : GuessANumber.java // 通過繼承 Thread 類創(chuàng)建線程 public class GuessANumber extends Thread { private int number; public GuessANumber(int number) { this.number = number; } public void run() { int counter = 0; int guess = 0; do { guess = (int) (Math.random() * 100 + 1); System.out.println(this.getName() + " guesses " + guess); counter++; }while(guess != number); System.out.println("** Correct! " + this.getName() + " in " + counter + " guesses.**"); } }
// 文件名 : ThreadClassDemo.java public class ThreadClassDemo { public static void main(String [] args) { Runnable hello = new DisplayMessage("Hello"); Thread thread1 = new Thread(hello); thread1.setDaemon(true); thread1.setName("hello"); System.out.println("Starting hello thread..."); thread1.start(); Runnable bye = new DisplayMessage("Goodbye"); Thread thread2 = new Thread(bye); thread2.setPriority(Thread.MIN_PRIORITY); thread2.setDaemon(true); System.out.println("Starting goodbye thread..."); thread2.start(); System.out.println("Starting thread3..."); Thread thread3 = new GuessANumber(27); thread3.start(); try { thread3.join(); }catch(InterruptedException e) { System.out.println("Thread interrupted."); } System.out.println("Starting thread4..."); Thread thread4 = new GuessANumber(75); thread4.start(); System.out.println("main() is ending..."); } }
運行結果如下,每一次運行的結果都不一樣。
Starting hello thread... Starting goodbye thread... Hello Hello Hello Hello Hello Hello Hello Hello Hello Thread-2 guesses 27 Hello ** Correct! Thread-2 in 102 guesses.** Hello Starting thread4... Hello Hello ..........remaining result produced.
2015職稱計算機考試書PowerPoint2007中 .. 定價:¥45 優(yōu)惠價:¥42 更多書籍 | |
2015年全國職稱計算機考試教材(2007模 .. 定價:¥225 優(yōu)惠價:¥213 更多書籍 |