建立執行緒的四種方式

2022-07-26 02:24:10 字數 4493 閱讀 6307

建立執行緒的四種方式

一、繼承thread類

繼承thread類建立執行緒的步驟為:

(1)建立乙個類繼承thread類,重寫run()方法,將所要完成的任務**寫進run()方法中;

(2)建立thread類的子類的物件;

(3)呼叫該物件的start()方法,該start()方法表示先開啟執行緒,然後呼叫run()方法;

public class thread1 } 

class threaddemo1 extends thread

}

二、實現runnable介面

實現runnable介面建立執行緒的步驟為:

(1)建立乙個類並實現runnable介面

(2)重寫run()方法,將所要完成的任務**寫進run()方法中

(3)建立實現runnable介面的類的物件,將該物件當做thread類的構造方法中的引數傳進去

(4)使用thread類的構造方法建立乙個物件,並呼叫start()方法即可執行該執行緒

public class thread2 

} class threaddemo2 implements runnable

}

三、實現callable介面

實現callable介面建立執行緒的步驟為:

(1)建立乙個類並實現callable介面

(2)重寫call()方法,將所要完成的任務的**寫進call()方法中,需要注意的是call()方法有返回值,並且可以丟擲異常

(3)如果想要獲取執行該執行緒後的返回值,需要建立future介面的實現類的物件,即futuretask類的物件,呼叫該物件的get()方法可獲取call()方法的返回值

(4)使用thread類的有參構造器建立物件,將futuretask類的物件當做引數傳進去,然後呼叫start()方法開啟並執行該執行緒。

public class thread3 

} class threaddemo3 implements callable

}

public class thread03 

});thread t = new thread(task);

t.start();

object obj = task.get();

system.out.println("執行緒執行的結果 = " + obj);

}}

四、使用執行緒池建立

使用執行緒池建立執行緒的步驟:

(1)使用executors類中的newfixedthreadpool(int num)方法建立乙個執行緒數量為num的執行緒池

(2)呼叫執行緒池中的execute()方法執行由實現runnable介面建立的執行緒;呼叫submit()方法執行由實現callable介面建立的執行緒

(3)呼叫執行緒池中的shutdown()方法關閉執行緒池

public class thread4 

}//實現runnable介面

class threaddemo4 implements runnable catch (interruptedexception e)

system.out.println(thread.currentthread().getname()+":"+"輸出的結果");

}}//實現callable介面

class threaddemo5 implements callable catch (interruptedexception e)

system.out.println(thread.currentthread().getname()+":"+"輸出的結果");

return thread.currentthread().getname()+":"+"返回的結果";

} }//實現runnable介面

class threaddemo6 implements runnable catch (interruptedexception e)

system.out.println(thread.currentthread().getname()+":"+"輸出的結果");

}}//實現runnable介面

class threaddemo7 implements runnable catch (interruptedexception e)

system.out.println(thread.currentthread().getname()+":"+"輸出的結果");

}}

一、繼承thread類

繼承thread類建立執行緒的步驟為:

(1)建立乙個類繼承thread類,重寫run()方法,將所要完成的任務**寫進run()方法中;

(2)建立thread類的子類的物件;

(3)呼叫該物件的start()方法,該start()方法表示先開啟執行緒,然後呼叫run()方法;

public class thread1 } 

class threaddemo1 extends thread

}

二、實現runnable介面

實現runnable介面建立執行緒的步驟為:

(1)建立乙個類並實現runnable介面

(2)重寫run()方法,將所要完成的任務**寫進run()方法中

(3)建立實現runnable介面的類的物件,將該物件當做thread類的構造方法中的引數傳進去

(4)使用thread類的構造方法建立乙個物件,並呼叫start()方法即可執行該執行緒

public class thread2 

} class threaddemo2 implements runnable

}

三、實現callable介面

實現callable介面建立執行緒的步驟為:

(1)建立乙個類並實現callable介面

(2)重寫call()方法,將所要完成的任務的**寫進call()方法中,需要注意的是call()方法有返回值,並且可以丟擲異常

(3)如果想要獲取執行該執行緒後的返回值,需要建立future介面的實現類的物件,即futuretask類的物件,呼叫該物件的get()方法可獲取call()方法的返回值

(4)使用thread類的有參構造器建立物件,將futuretask類的物件當做引數傳進去,然後呼叫start()方法開啟並執行該執行緒。

public class thread3 

} class threaddemo3 implements callable

}

public class thread03 

});thread t = new thread(task);

t.start();

object obj = task.get();

system.out.println("執行緒執行的結果 = " + obj);

}}

四、使用執行緒池建立

使用執行緒池建立執行緒的步驟:

(1)使用executors類中的newfixedthreadpool(int num)方法建立乙個執行緒數量為num的執行緒池

(2)呼叫執行緒池中的execute()方法執行由實現runnable介面建立的執行緒;呼叫submit()方法執行由實現callable介面建立的執行緒

(3)呼叫執行緒池中的shutdown()方法關閉執行緒池

public class thread4 

}//實現runnable介面

class threaddemo4 implements runnable catch (interruptedexception e)

system.out.println(thread.currentthread().getname()+":"+"輸出的結果");

}}//實現callable介面

class threaddemo5 implements callable catch (interruptedexception e)

system.out.println(thread.currentthread().getname()+":"+"輸出的結果");

return thread.currentthread().getname()+":"+"返回的結果";

} }//實現runnable介面

class threaddemo6 implements runnable catch (interruptedexception e)

system.out.println(thread.currentthread().getname()+":"+"輸出的結果");

}}//實現runnable介面

class threaddemo7 implements runnable catch (interruptedexception e)

system.out.println(thread.currentthread().getname()+":"+"輸出的結果");

}}

建立執行緒的四種方式

1.繼承於thread類,重寫run 方法 2.實現runable介面,實現裡面的run 方法 前兩種不用多說 3.使用 futuretask 實現有返回結果的執行緒,可以返回執行緒執行結果 public class test class mycallable implements callable...

建立執行緒的四種方式

class thread00 extends thread class thread01 implements runnable class thread02 implements callable 對以上三種方法進行測試 測試四種建立執行緒的方式 public class newthread 輸出...

建立執行緒的四種方式

首先我們先來了解一下測試thread中的常用方法 一 繼承thread類 二 實現runnable介面 三 實現callable介面 四 使用執行緒池建立 建立執行緒方式一 繼承thread類 繼承thread類建立執行緒的步驟為 1 新建乙個主類,然後再建立乙個類繼承thread的子類。2 重寫t...