ThreadLocal类,实例测试,FutureTask类,实例测试。
2024-08-24 02:01:58
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
153147614573
name:myname password:mypassword
153147614574
最新文章
- [leetcode] 题型整理之图论
- (十八)WebGIS中清空功能和地图定位功能的设计以及实现
- 动态SQL字符长度超过8000
- java spring hibernate
- Highcharts结合PhantomJS在服务端生成高质量的图表图片
- html img src base64
- easyUI数据表格datagrid之笔记2
- Sturts2的action不执行任何方法的原因
- 1047. Student List for Course (25)
- Eclipse设置注释模板
- web app 自适应 弹性布局之rem
- DBA 培训相应内容笔记
- Flask中全局变量的实现
- RH253读书笔记(3)-Lab 3 Securing Networking
- codevs1051
- MySQL在字段中使用select子查询
- 《java入门第一季》之面向对象(匿名内部类)
- Mysql查看登录用户以及修改密码和创建用户以及授权(转载)
- 异常:No Spring WebApplicationInitializer types detected on classpath
- [Spark][kafka]kafka 的topic 创建和删除试验