如何给run()方法传参数
实现的方式主要有三种
1、构造函数传参
2、成员变量传参
3、回调函数传参
问题:如何实现处理线程的返回值?
1、主线程等待法(优点:实现起来简单,缺点:需要等待的变量一多的话,代码就变的非常臃肿。而且不能精准控制时间)
public class CycleWait implements Runnable{
private String value;
public void run() {
try {
Thread.currentThread().sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
value = "we have data now";
}
public static void main(String[] args) throws InterruptedException {
CycleWait cw = new CycleWait();
Thread t = new Thread(cw);
t.start();
// while (cw.value == null){
// Thread.currentThread().sleep(100);
// }
t.join();
System.out.println("value : " + cw.value);
}
}
2、使用Thread类的join()阻塞当前线程以等待子线程处理完毕(缺点:精准度不够)
3、通过Callable接口实现:通过FutureTask Or 线程池获取
public class MyCallable implements Callable<String> {
@Override
public String call() throws Exception{
String value="test";
System.out.println("Ready to work");
Thread.currentThread().sleep(5000);
System.out.println("task done");
return value;
}
}
FutureTask 实现方式
public class FutureTaskDemo {
public static void main(String[] args) throws ExecutionException, InterruptedException {
FutureTask<String> task = new FutureTask<String>(new MyCallable());
new Thread(task).start();
if(!task.isDone()){
System.out.println("task has not finished, please wait!");
}
System.out.println("task return: " + task.get());
}
}
线程池实现方式
public class ThreadPoolDemo {
public static void main(String[] args) {
ExecutorService newCachedThreadPool = Executors.newCachedThreadPool();
Future<String> future = newCachedThreadPool.submit(new MyCallable());
if(!future.isDone()){
System.out.println("task has not finished, please wait!");
}
try {
System.out.println(future.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} finally {
newCachedThreadPool.shutdown();
}
}
}
如何给run()方法传参数的更多相关文章
- javascript 利用匿名函数对象给你异步回调方法传参数
先来创建一个匿名函数对象: /*** * 匿名函数 */ var callChangeBtn=new function(bugBtn){ this.chage=function(json){ bugB ...
- XMLHTTP中setRequestHeader方法和参数
注意:在FF里面需要将open方法放在setRequestHeader之前 一.为何要用到setRequestHeader 通 常在HTTP协议里,客户端像服务器取得某个网页的时候,必须发送一个HTT ...
- Java:方法的参数是传值还是传引用
Java中方法的参数总是采用传值的方式. 下列方法欲实现对象的交换,但实际上是不能实现的. public void swap(simpleClass a,simpleClass b){ simpleC ...
- mvc中多参数URL会很长,首次加载不传参数让url很短,路由规则实现方法[bubuko.com]
如要实现列表中地址全路径“bubuko-11-2.html”,在首次进入时,使用短路径“bubuko.html”,只有再次href后才显示全路径“bubuko-11-2.html”,下面使用路由规则来 ...
- .net中以传引用的方式 向方法中传参数
CLR(CommonLanguageRuntime)公共语言运行时,允许以传引用而非传值的方式传递参数.在C#中,这是用关键字 out 和ref来做到的. 从CLR角度来看,这两个关键字没什么区别,生 ...
- 【ASP.NET Core】给中间件传参数的方法
最近博客更新频率慢了些,原因有三: 其一,最近老周每星期六都录 ASP.NET Core 的直播,有些内容在视频里讲过,就不太想在博客里面重复.有兴趣的话可以去老周的微博看,或者去一直播,直播帐号与微 ...
- mybatis(3)---传参数的方法
1.传一个参数 //接口方法List<EmpVo> find(int empId); //xml配置 <select resultType="com.ht.mapper.E ...
- 编程写一个方法时,注意方法中传参数的数量最好不要超过5个,超过5个怎么办?可以用struct或class,或一个字典类
图 1 一.从图1发现了什么问题呢? 答案:1.参数传的的太多了:2.另外注释也没写好. 说明:一个方法中,传参数的数量最好不要超过5个. 应该采用:struct或class,或一个字典类都行.其中 ...
- ASP.NET MVC WebApi 返回数据类型序列化控制(json,xml) 用javascript在客户端删除某一个cookie键值对 input点击链接另一个页面,各种操作。 C# 往线程里传参数的方法总结 TCP/IP 协议 用C#+Selenium+ChromeDriver 生成我的咕咚跑步路线地图 (转)值得学习百度开源70+项目
ASP.NET MVC WebApi 返回数据类型序列化控制(json,xml) 我们都知道在使用WebApi的时候Controller会自动将Action的返回值自动进行各种序列化处理(序列化为 ...
随机推荐
- django上课笔记5-FK关联
一.FK关联 FK关联 url.py里 from app01 import views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r' ...
- 010-- 开发脚本自动部署nginx_web和nfs及监控内存
1.编写脚本自动部署反向代理.web.nfs: #!/bin/bash #检测安装nginx function detection_nginx(){ if [ -f /etc/nginx/nginx. ...
- python学习笔记1-基础语法
1 在3版本中print需要加上括号2 多行语句:用\连接 item_one=1 item_two=2 item_three=3 total = item_one + \ item_two + \ i ...
- 洛谷 - P1403 - 约数研究 - 数论
https://www.luogu.org/problemnew/show/P1403 可以直接用线性筛约数个数求出来,但实际上n以内i的倍数的个数为n/i的下整,要求的其实是 $$\sum\limi ...
- HK算法模板+小优化(跑的快一点点)
HUST 2604 #include <iostream> #include <cstdlib> #include <cstdio> #include <cs ...
- 序列/树上差分小结 By cellur925
首先我们需要注意一下的是,差分比较适用于修改比较多而查询比较少的情况. 一.序列上差分 借教室 这是一道二分答案,在check函数中用到差分技巧的一道题,譬如说我们要把一个序列中[l,r]区间都加上 ...
- Hibernate中表与表之间的关联多对多,级联保存,级联删除
第一步:创建两个实体类:用户和角色实体类,多对多关系,并让两个实体类之间互相关联: 用户实体类: package com.yinfu.entity; import java.util.HashSet; ...
- hdu 3172 Virtual Friends (字符串的并查集)
一开始一直wa,因为第一个数字t也是多组输入. 然后一直超时,因为我用的是C++里面的cin,所以非常耗时,几乎比scanf慢了10倍,但是加上了一个语句后: std::ios::sync_with_ ...
- the little schemer 笔记(10)
第十章 What Is the Value of All of This? entry条目 是由list表组成的 pair 对,pair 对的第一个list表是集合 set.另外,两个list表的长 ...
- _bzoj1096 [ZJOI2007]仓库建设【斜率优化dp】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1096 又是一道经典斜率优化. #include <cstdio> const i ...