更新時間:2021-10-09 10:57:45 來源:動力節點 瀏覽1473次
通過擴展 Thread 類
通過實現 Runnable 接口。
Thread 類提供構造函數和方法來創建和執行線程上的操作。Thread 類擴展了 Object 類并實現了 Runnable 接口。
Thread類的常用構造函數:
線()
線程(字符串名稱)
線程(可運行 r)
線程(Runnable r,字符串名稱)
public void run():用于為線程執行操作。
public void start():開始線程的執行。JVM在線程上調用run()方法。
public void sleep(long miliseconds):使當前正在執行的線程休眠(暫時停止執行)指定的毫秒數。
public void join():等待一個線程死亡。
public void join(long miliseconds):等待一個線程在指定的毫秒內死亡。
public int getPriority():返回線程的優先級。
public int setPriority(int priority):改變線程的優先級。
public String getName():返回線程的名稱。
public void setName(String name):更改線程的名稱。
public Thread currentThread():返回當前執行線程的引用。
public int getId():返回線程的id。
public Thread.State getState():返回線程的狀態。
public boolean isAlive():測試線程是否存活。
public void yield():使當前正在執行的線程對象暫時暫停并允許其他線程執行。
public void suspend():用于掛起線程(已棄用)。
public void resume():用于恢復掛起的線程(已棄用)。
public void stop():用于停止線程(已棄用)。
public boolean isDaemon():測試線程是否為守護線程。
public void setDaemon(boolean b):將線程標記為守護進程或用戶線程。
public void interrupt():中斷線程。
public boolean isInterrupted():測試線程是否被中斷。
public static boolean interrupted():測試當前線程是否被中斷。
Runnable 接口應該由其實例旨在由線程執行的任何類實現。Runnable 接口只有一個名為 run() 的方法。
public void run():用于為線程執行操作。
Thread 類的start() 方法用于啟動新創建的線程。它執行以下任務:
一個新線程開始(使用新的調用堆棧)。
線程從 New 狀態移動到 Runnable 狀態。
當線程有機會執行時,它的目標 run() 方法將運行。
1.通過擴展 Thread 類的 Java 線程示例
文件名: Multi.java
class Multi extends Thread{
public void run(){
System.out.println("thread is running...");
}
public static void main(String args[]){
Multi t1=new Multi();
t1.start();
}
}
輸出:
線程正在運行...
2.通過實現 Runnable 接口的 Java 線程示例
文件名: Multi3.java
class Multi3 implements Runnable{
public void run(){
System.out.println("thread is running...");
}
public static void main(String args[]){
Multi3 m1=new Multi3();
Thread t1 =new Thread(m1); // Using the constructor Thread(Runnable r)
t1.start();
}
}
輸出:
線程正在運行...
如果您不擴展 Thread 類,則您的類對象不會被視為線程對象。所以需要顯式創建Thread類對象。我們正在傳遞實現 Runnable 的類的對象,以便您的類 run() 方法可以執行。
3.使用線程類:Thread(String Name)
我們可以使用上面定義的構造函數直接使用 Thread 類來生成新線程。
文件名: MyThread1.java
public class MyThread1
{
// Main method
public static void main(String argvs[])
{
// creating an object of the Thread class using the constructor Thread(String name)
Thread t= new Thread("My first thread");
// the start() method moves the thread to the active state
t.start();
// getting the thread name by invoking the getName() method
String str = t.getName();
System.out.println(str);
}
}
輸出:
我的第一個線程
(4)使用線程類:Thread(Runnable r, String name)
觀察以下程序。
文件名: MyThread2.java
public class MyThread2 implements Runnable
{
public void run()
{
System.out.println("Now the thread is running ...");
}
// main method
public static void main(String argvs[])
{
// creating an object of the class MyThread2
Runnable r1 = new MyThread2();
// creating an object of the class Thread using Thread(Runnable r, String name)
Thread th1 = new Thread(r1, "My new thread");
// the start() method moves the thread to the active state
th1.start();
// getting the thread name by invoking the getName() method
String str = th1.getName();
System.out.println(str);
}
}
輸出:
我的新線程
現在線程正在運行...
如果大家對此感興趣,想了解更多相關知識,可以關注一下動力節點的Java多線程編程教程,里面的內容豐富,通俗易懂,適合沒有基礎的朋友學習,希望對大家能夠有所幫助。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習