1、繼承thread類
2、實現runnable介面
3、定時器
timer timer = new timer();
timer.schedule(new timertask()
}, format.parse("2017-10-11 22:00:00"));
4、通過callable和futuretask建立執行緒
callablecall = new callable()
};5、基於執行緒池的方式
// 建立執行緒池
executorservice threadpool = executors.newfixedthreadpool(10);
while(true) catch (interruptedexception e)
}});
}6、spring的非同步任務@async
@async
public void b()
呼叫該方法是非同步的,不會因為這個方法阻塞
7、匿名內部類
new thread(){
publivc void run(){
Java中實現多執行緒的倆種方式
方法一 繼承thread類,重寫run 方法 public class testrun catch interruptedexception e class thread extends thread catch interruptedexception e 開啟子執行緒,與主線程併發執行,設定不同...
Java多執行緒實現的四種方式
方式1 繼承thread類的執行緒實現方式如下 public class threaddemo01 extends thread public void run public static void main string args 程式結果 thread main,5,main 我是自定義的執行緒...
JAVA多執行緒的四種實現方式
1.繼承thread 重寫run 方法,該run方法表示執行緒要完成的任務。建立執行緒物件,呼叫物件的start 方法來啟動執行緒。2.runnable介面 重寫介面中run方法。建立runable例項類的例項,並依此例項作為thread的target來建立thread物件,該執行緒物件才是真正的執...