java创建线程方式

时间: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无返回值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无返回值,使命一旦提交,无法在当前线程中监控履行结果。submit有一个Future类型的返回值,用来接纳返回值或响应反常。通过get()办法获取。
submit底层还是调用的execute,只是在此基础上用future封装了一层,并将履行过程中发生的反常全部封装在一个变量中:
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注解学习使用了submit的处理办法。
5.匿名内部类
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表达式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());//无返回值
Futurefuture=service.submit(()->execute());//有返回值
list.add(future);
}publicstaticvoidexecute(){//dosomething}
}

文章标签:

Copyright © 2016 广州思洋文化传播有限公司,保留所有权利。 粤ICP备09033321号

与项目经理交流
扫描二维码
与项目经理交流
扫描二维码
与项目经理交流
ciya68