首页 > 编程知识 正文

多线程的原理以及如何实现,多线程的几种实现方法

时间:2023-05-03 05:15:02 阅读:17426 作者:3801

要实现多线程,请继承Thread类

实现Runnable接口

可调用接口的实现

4 .线程池:提供包含所有等待线程的线程队列。 避免创建和销毁额外成本,缩短响应时间。

体系结构:

Java.util.concurrent.executor :是负责线程使用和调度的根接口

|--ExecutorService子接口:线程池的主要接口

|--线程池实现类

|--ScheduledExecutorService子接口:负责调度线程

|-- scheduledthreadpoolexecutor :接管thread pool executor,ScheduledExecutorService*

工具类:执行程序

executorservicenewfixedthreadpool () :创建固定大小的线程池

executorservicenewcachedthreadpool () :缓存线程池。 线程池的数量不是固定的,可以根据需要自动更改数量。

executorservicenewsinglethreadexecutor () :创建单个线程池。 线程池中只有一个线程

scheduledexecutorservicenewscheduledthreadpool () :可以创建固定大小的线程,以延迟或调度任务的执行。

1 )继承Thread类package com.lxj.juc; 公共类测试thread { publicstaticvoidmain (string [ ] args ) threaddemothreaddemo=new thread demo ); threadDemo.start (; } classthreaddemoextendsthread { @ overridepublicvoidrun (} { boolean flag=false; for(intI=3; i 100; I ) {flag=false; for(intj=2; j=math.sqrt(I; j ) if(I%j==0) {flag=true; 布雷克; }if(flag==false ) system.out.print(I ' ); }}}运行结果: 3571113171923293137414143475359617173838997 )实现Runnable接口package com.lxj.juc; publicclasstestrunnable { publicstaticvoidmain (字符串[ ] args ) runnabledemorunnabledemo=newrunnabledemo ); 新thread (runnable demo ).start ); } classrunnabledemoimplementsrunnable { @ overridepublicvoidrun (} { boolean flag=false; for(intI=3; i 100; I ) {flag=false; for(intj=2; j=math.sqrt(I; j ) if(I%j==0) {flag=true; 布雷克; }if(flag==false ) system.out.print(I ' ); }}}运行结果:3511317192329313741414347435359677173838997 )实现Callable接口package com.lxj.juc; import java.util.ArrayList; import java.util.List; import Java.util.concurrent.callable; import Java.util.concurrent.execution exception; import Java.util.concurrent.executors; import Java.util.concurrent.future task; publicclasstestcallable1{ publicstaticvoidmain (字符串[ ] args ) throws InterruptedException,execution exception { calllable } newthread(futuretask ).start ); listintegerlists=(listinteger ) futureTask.get ); //返回值for(integerinteger:lists ) system.out.print (integer ' ); } } classcallabledemoimplementscallablelistinteger { @ overridepubliclistintegercall () Throwsexception ) boolean flag=fale for(intI=3; i 100; I ) {flag=false; for(intj=2; j=math.sqrt(I; j ) if(I%j==0) {flag=true; 布雷克; }if(flag==false ) lists.add ) I; } }返回列表; }执行结果:357113171923293137414143435359596171738389974 )线程池package com.lxj.juc; import java.util.ArrayList; import java.util.List; import Java.util.concurrent.callable; import Java.util.concurrent.execution exception; import Java.util.concurrent.executorservice; import Java.util.concurrent.executors; import Java.util.concurrent.future; publicclasstestthreadpool { publicstaticvoidmain [ ] args } throwsinterruptedexception,execution exception { execu ton }列表for(intI=0; i 5; I ) ) futurelistintegerfuture=executorservice.submit (newcallablelistinteger () { @ override公共列表集成器呼叫) ) the 列表integer lists=new ArrayList (; for(intI=3; i 100; I ) {flag=false; for(intj=2; j=math.sqrt(I; j ) if(I%j==0) {flag=true; 布雷克; }if(flag==false ) lists.add ) I; } }返回列表; }; ); ints.add(future; } for (futurelistintegerfuture : ints ) system.out.println ) future.get (); }}}class ThreadPoolDemo {}运行结果: pool-1-thread-2 pool-1-thread-5 pool-1-thread-3 pool-1-thread-1 pool-1 13、17、19、23、29、31、37、41 ] 97 ) [3、5、7、11、13、17、19、23、29、31、41、43、53、59、61、67

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。