V get(Long timeout,TimeUnit unit):获取异步执行功效,假如没哟功效可用,此要了解阻塞,可是会有时间限制,假如阻塞时间高出设定的timeout时间,该要领将抛出异常;
boolean isDone():假如任务执行竣事,无论是正常竣事或是半途打消照旧产生异常,都返回true;
boolean isCanceller():假如任务完成前被打消,则返回true;
boolean cancel(boolean mayInterrupRunning):假如任务还没有开始,执行cancel要领将返回false;假如任务已经启动,执行cancel要领将以间断执行此任务线程的方法来试图遏制任务,假如遏制乐成,返回true;
当任务已经启动,执行cancel(false)要领将不会对正在执行的任务线程发生影响(让线程正常执行到完成),此时返回false;
当任务已经启动,执行cancel要领将返回false,MayInterruptRunning参数暗示是否间断执行中的线程;
实际上Future提供了三种成果:
1.可以或许间断执行中的任务;
2.判定任务是否执行完成;
3.获取任务执行完成后的功效;
5.线程池建设的方法 5.1 newCachedThreadPoolnewCachedThreadPool:可缓存线程池,先查察池中有没有以前成立的线程,假如有,就直接利用;假如没有,就建设一个新的线程插手池中,缓存型池凡是用于执行一些保留期很短的异步型任务;
package com.wn.threadpool; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class NewCachedThreadPoolTest { //无限大侠线程池,JVM自动接纳 public static void main( String[] args ){ ExecutorService newCachedThreadPool = Executors.newCachedThreadPool(); for (int i=1;i<=10;i++){ newCachedThreadPool.execute(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName()); } }); } } }
5.2 newFixedThreadPoolnewFixedThreadPool:建设一个可重用牢靠个数的线程池,以共享的无界行列方法来运行这些线程;
package com.wn.threadpool; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class NewFixedThreadPoolTest { public static void main( String[] args ){ ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(3); for (int i=0;i<10;i++){ newFixedThreadPool.execute(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName()); } }); } } }
5.3 newScheduledThreadPoolnewScheduledThreadPool:建设一个定长线程池,支持按时及周期性任务执行
package com.wn.threadpool; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; public class NewScheduledThreadPoolTest { public static void main( String[] args ) { ScheduledExecutorService newScheduledThreadPool = Executors.newScheduledThreadPool(3); for (int i=0;i<10;i++){ newScheduledThreadPool.schedule(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName()); } },3, TimeUnit.SECONDS); } } }
5.4 newSingleThreadExecutornewSingleThreadExecutor:建设一个单线程化的线程池,它只会用独一的事情线程来执行任务,担保所有任务凭据指定顺序执行;
package com.wn.threadpool; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class NewSingleThreadExecutorTest { public static void main( String[] args ){ ExecutorService newSingleThreadExecutor = Executors.newSingleThreadExecutor(); for (int i=0;i<10;i++){ final int index=i; newSingleThreadExecutor.execute(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName()+"正在被执行,index:"+index); try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } } }); } } }
6.Java傍边线程状态有哪些6.1 初始状态(New):
线程工具建设出来后,没有挪用start要领,线程处于初始状态;
6.2 运行状态:
1.停当状态(Ready):挪用了Start要领,期待CPU分派资源;
2.运行状态(RUNNING):CPU分派资源给该线程,该线程处于运行状态;
6.3 阻塞状态 (BLOCKED):