ThreadLocal类,实例测试,FutureTask类,实例测试。
1:测试ThreadLocal类, 为每个线程域保存局部变量。例如下面的例子。
ThreadLocal为每个线程保存了一个Test对象, 那么当执行线程时,每个线程中的test具有唯一性。某一个线程执行时,查询当前线程是否在ThreadLocalMap是否具有Test缓存对像,判断出该线程具有ThreadLocal保存的Test对象时,就不再创建Test对象,使用ThreadLocal为本线程保存的Test对象。如果没有就创建一个。
package chapter03; import java.util.concurrent.Executor; import java.util.concurrent.ThreadPoolExecutor; /** * @program: GradleTestUseSubModule * @author: Yafei Li * @create: 2018-07-12 16:14 **/ public class ThreadLocalTest { static ThreadLocal<Test> threadLocal=new ThreadLocal(){ public Test initialValue() { return new Test(); } }; public static void main(String[] args){ Thread thread = new Thread(new Runnable() { @Override public void run() { while (true) { Test test = threadLocal.get(); test.countplus(); System.out.println(Thread.currentThread()+" "+test.i); if (test.i > 5) { break; } } } }); Thread thread2 = new Thread(new Runnable() { @Override public void run() { while (true) { Test test = threadLocal.get(); test.countplus(); System.out.println(Thread.currentThread()+" "+test.i); if (test.i > 5) { break; } } } }); thread.start(); thread2.start(); /** * 结果 Thread[Thread-0,5,main] 1 Thread[Thread-1,5,main] 1 Thread[Thread-1,5,main] 2 Thread[Thread-1,5,main] 3 Thread[Thread-1,5,main] 4 Thread[Thread-1,5,main] 5 Thread[Thread-1,5,main] 6 Thread[Thread-0,5,main] 2 Thread[Thread-0,5,main] 3 Thread[Thread-0,5,main] 4 Thread[Thread-0,5,main] 5 Thread[Thread-0,5,main] 6 */ } } class Test{ int i=0; public void countplus() { i++; } }
2:FutureTask表示线程一个将来的结果,方法会等到该线程得到结果为止。
例如:
package chapter05.class5; import org.junit.Test; import java.util.concurrent.Callable; import java.util.concurrent.FutureTask; /** * @program: GradleTestUseSubModule * @author: Yafei Li * @create: 2018-07-13 17:24 **/ public class MyPreloader { //FutureTask 异步结果,闭锁 private final FutureTask<ProductInfo> future = new FutureTask<ProductInfo>(new Callable<ProductInfo>() { @Override public ProductInfo call() throws Exception { System.out.println("执行thread中的future"); System.out.println(System.currentTimeMillis()); Thread.sleep(10000); ProductInfo productInfo=new ProductInfo("myname","mypassword"); return productInfo; } }); private final Thread thread = new Thread(future); //FutureTask实现了Runnable类。 public void start(){ thread.start(); } public ProductInfo get() { try { return future.get(); //会等待线程执行,直到返回结果。 }catch (Exception e){ e.printStackTrace(); } return null; } @Test public void test() { thread.start(); //执行thread,得到FutureTask ProductInfo productInfo=get(); //会等待线程执行,直到得到返回的结果 System.out.println("name:"+productInfo.getName()+" password:"+productInfo.getPassword()); System.out.println(System.currentTimeMillis()); } }
ProductInfo类
package chapter05.class5; public class ProductInfo { private String name; private String password; public ProductInfo(String name, String password) { this.name = name; this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
结果:
执行thread中的future 1531476154573 name:myname password:mypassword 1531476164574
本博客为非营利性个人原创,除部分有明确署名的作品外,所刊登的所有作品的著作权均为本人所拥有,本人保留所有法定权利。违者必究