- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在 Runnable 上设置参数然后获取值。
我写了这段代码。当我运行此代码时,返回 [2, 3, 3]。因为线程共享temp_value。
然后我添加了 sleep ,但它被注释掉了。结果是[1, 2, 3]。它工作正常!但是..它不是真正的多线程,对吧?
即使它正在运行多线程,但我需要等待每个进程完成共享值。
如何解决这个问题?
import java.util.ArrayList;
public class Foo implements Runnable {
private int temp_value;
private ArrayList<Integer> values = new ArrayList<Integer>();
private ArrayList<Integer> newValues = new ArrayList<Integer>();
public Foo(ArrayList<Integer> values) {
this.values = values;
}
public static void main(String[] args) {
// make initial values
ArrayList<Integer> values = new ArrayList<Integer>();
values.add(1);
values.add(2);
values.add(3);
// set values then process and get new values
Foo foo = new Foo(values);
foo.startAppendValue(foo);
System.out.println(foo.getNewValues());
}
public void startAppendValue(Foo foo) {
Thread thread = null;
int max = values.size();
for (int i = 0; i < max; i++) {
foo.temp_value =foo.values.get(i);
thread = new Thread(foo);
thread.start();
// try {
// Thread.sleep(10);
// } catch (Exception e) {
// // TODO: handle exception
// }
}
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public void run() {
newValues.add(temp_value);
}
public ArrayList<Integer> getNewValues() {
return this.newValues;
}
}
最佳答案
您可以使用 Callable
和 ExecutorService
来做这些事情
public class MyCallable implements Callable<Integer> { //Callable is like Runnable but can return value
private Integer value;
public MyCallable(Integer v) {
value = v;
}
public Integer call() {
return value;
}
public static void main(String[] args) {
ExecutorService exec = Executors.newFixedThreadPool(3); //Creating thread pool with 3 worker threads
List<Integer> values = Arrays.asList(1, 2, 3);
List<Future<Integer>> futures = new ArrayList<>(values.size());
List<Integer> newValues = new ArrayList<>(values.size());
for (Integer v : values) {
futures.add(exec.submit(new MyCallable(v))); //Submit tasks to worker threads to do stuff in background
}
for (Future<Integer> f : futures) {
try {
newValues.add(f.get()); // get calculated result from worker thread or block waiting for result to become available
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
System.out.println(newValues);
exec.shutdownNow();
}
}
关于java - 如何在 Runnable 上设置参数然后获取值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52469093/
这个问题在这里已经有了答案: final keyword in method parameters [duplicate] (9 个回答) 关闭 8 年前。 在此示例中,声明 Object fina
我的目标:是通过我的函数更新字段获取选定值并使用函数输出值运行它。 问题:当我从列表中选择值时,它不会触发函数,也不会更新字段。 感谢您的帮助。 HTML 12 14 16 18 20 22 24
我有一本具有这种形式的字典: myDict = {'foo': bar, 'foobar baz': qux} 现在,我想拆分字典键中的空格,使其成为下一个键并获取值(重复)。 myDictRev1
vector a; vector b; int temp_holder; cout > temp_holder) a.push_back(temp_holder); cout > temp_h
Java 的开发过程中免不了与 Date 类型纠缠,准备总结一下项目经常使用的日期相关操作,JDK 版本 1.7,如果能够帮助大家节约那么几分钟起身活动一下,去泡杯咖啡,便是极好的,嘿嘿。当然,我
我正在使用 jquery ui 日期选择器来获取 fromDate 和 toDate 以下是from日期的代码 $("#from_date").datepicker({
我是一名优秀的程序员,十分优秀!