色欲av一区久久精品_久久综合色综合色88_无码在线观看不卡_色黄视频网站_亚洲国产精品久久久久秋霞66

java創(chuàng)建線程方式

時(shí)間:2023-05-16

1.繼承Thread類
publicclassThreadCreatorextendsThread{publicstaticvoidmain(String[]args){
//第一種辦法:
ThreadCreatorcreator=newThreadCreator();
Threadthread=newThread(creator,”線程1″);
thread.start();
//第二種辦法:
Threadthread=newThreadCreator();
thread.start();Java
//第三種辦法:
newThreadCreator().start();
}
@Overridepublicvoidrun(){
System.out.println(Thread.currentThread().getName()+”run”);
}
}
2.完成Runnable接口
publicclassThreadCreatorimplementsRunnable{publicstaticvoidmain(String[]args){
ThreadCreatorcreator=newThreadCreator();
Threadthread=newThread(creator,”線程1″);
thread.start();
}
@Overridepublicvoidrun(){
System.out.println(Thread.currentThread().getName()+”run”);
}
}
3.完成Callable接口
publicclassThreadCreatorimplementsCallable{publicstaticvoidmain(String[]args)throwsExecutionException,InterruptedException{
ThreadCreatorcreator=newThreadCreator();
FutureTaskfutureTask=newFutureTask(creator);
Threadthread=newThread(futureTask,”線程”);
thread.start();
System.out.println(futureTask.get());
}
@OverridepublicIntegercall(){return1024;
}
}
4.線程池ExecutorService
publicclassThreadCreator{staticExecutorServiceservice=Executors.newFixedThreadPool(5);publicstaticvoidmain(String[]args)throwsExecutionException,InterruptedException{//execute無(wú)返回值service.execute(newThreadTask(1,”1″));//submit有返回值Futureresult=service.submit(newThreadTaskCall());
System.out.println(result.get());
service.shutdownNow();
}staticclassThreadTaskimplementsRunnable{privateintparam1;privateStringparam2;publicThreadTask(intparam3,Stringparam4){this.param1=param3;this.param2=param4;
}
@Overridepublicvoidrun(){
System.out.println(param1+param2);
}
}staticclassThreadTaskCallimplementsCallable{
@OverridepublicIntegercall()throwsException{return1024;
}
}
}
線程池中submit和execute的差異:
①可承受的使命類型不一樣:execute只能承受Runnable使命,submit還可以承受Callable使命。
②返回值:execute無(wú)返回值,使命一旦提交,無(wú)法在當(dāng)前線程中監(jiān)控履行結(jié)果。submit有一個(gè)Future類型的返回值,用來(lái)接納返回值或響應(yīng)反常。通過(guò)get()辦法獲取。
submit底層還是調(diào)用的execute,只是在此基礎(chǔ)上用future封裝了一層,并將履行過(guò)程中發(fā)生的反常全部封裝在一個(gè)變量中:
publicvoidrun(){if(state!=NEW||
!UNSAFE.compareAndSwapObject(this,runnerOffset,null,Thread.currentThread()))return;try{
Callablec=callable;if(c!=null&&state==NEW){
Vresult;booleanran;try{
result=c.call();
ran=true;
}catch(Throwableex){
result=null;
ran=false;
setException(ex);
}if(ran)
set(result);
}
}finally{
runner=null;ints=state;if(s>=INTERRUPTING)
handlePossibleCancellationInterrupt(s);
}
}protectedvoidsetException(Throwablet){if(UNSAFE.compareAndSwapInt(this,stateOffset,NEW,COMPLETING)){
outcome=t;
UNSAFE.putOrderedInt(this,stateOffset,EXCEPTIONAL);//finalstatefinishCompletion();
}
}
另外,spring中的schedule注解學(xué)習(xí)使用了submit的處理辦法。
5.匿名內(nèi)部類
publicclassThreadCreator{publicstaticvoidmain(String[]args){//繼承Thread類newThread(){
@Overridepublicvoidrun(){System.out.println(“extendsThreadClass!”);
}
}.start();//完成Runnable接口newThread(newRunnable(){
@Overridepublicvoidrun(){
System.out.println(“implementRunnable!”);
}
}).start();//完成Callable接口newThread(newFutureTask(newCallable(){
@OverridepublicIntegercall()throwsException{return1024;
}
})).start();//lambda表達(dá)式newThread(()->System.out.println(“executesinglecode”)).start();newThread(()->{
System.out.println(“executemultiplecode”);
}).start();
}
}
lambda線程池:
publicclassThreadCreator{staticExecutorServiceservice=Executors.newFixedThreadPool(5);staticListlist=newArrayList();publicstaticvoidmain(String[]args){
service.execute(()->execute());//無(wú)返回值
Futurefuture=service.submit(()->execute());//有返回值
list.add(future);
}publicstaticvoidexecute(){//dosomething}
}

文章標(biāo)簽:

Copyright ? 2016 廣州思洋文化傳播有限公司,保留所有權(quán)利。 粵ICP備09033321號(hào)

與項(xiàng)目經(jīng)理交流
掃描二維碼
與項(xiàng)目經(jīng)理交流
掃描二維碼
與項(xiàng)目經(jīng)理交流
ciya68