首页 > 编程知识 正文

钢琴入门自学教程,唱歌入门最简单自学

时间:2023-05-03 17:39:59 阅读:42496 作者:3361

newSingleThreadExecutor创建单线程池,只在唯一的工作线程上执行任务,以确保所有任务都按指定顺序(FIFO、LIFO、优先级)执行。

Runable类:

publicclassrunthreadimplementsrunnable {私有int count=0; @ overridepublicvoidrun ((布尔标志=true; while(flag ) system.out.println('hello!' ); 出局; if (计数==3) { flag=false; } }测试类:

公共类测试{ publicstaticvoidmain (字符串[ ] args ) runthreadrunthread=newrunthread ); executorserviceexecutor=executors.newsinglethreadexecutor (; 执行程序.执行程序(运行程序; futureliststringfuture=executor.submit (new list callable (); try { liststringlist=future.get (60l,TimeUnit.MINUTES ); system.out.println (' the result==' list.tostring ) ); }catch(exceptione ) { e.printStackTrace ); }隐私保护列表{ @ overridepublicliststringcall (throws exception )列表保护列表}}输出结果:

你好!

你好!

你好!

the result==[张gsan,lisi,wangwu]

提交(和执行)之间的差异:

publicinterfaceexecutor { void execute (可运行命令; }参与} execute ()方法为Runnable,返回值为void

publicinterfaceexecutorserviceextendsexecutor { . tfuturetsubmit (callablettask; tfuturetsubmit (运行任务,T result ); YouTube? 提交(运行任务); ExecutorService接口具有以上三个sumbit )方法。 参与为CallableT或Runnable,方法具有返回值FutureT。

1、submit ) )有返回值,execute ) )中没有。

2、任务抛出checked或unchecked exception,如果外部调用方想感知这些exception并及时处理,请使用submit,Future.get ) )进行异常缓存

Executors创建线程的其他方式:

newCachedThreadPool创建一个可缓存的线程池。 如果线程池的长度超过需要处理的长度,请灵活地重用空闲线程,如果无法重用,请创建新线程。 newFixedThreadPool创建了一个固定长度的线程池,用于控制线程的最大并发行数,超出的线程将在队列中等待。 newScheduledThreadPool创建固定长度的线程池,以支持计划和定期任务执行。

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