如何在 Runnable 上设置参数然后获取值?



我想在 Runnable 上设置参数然后获取值。

我写了这段代码。当我运行此代码时,返回[2, 3, 3]。因为线程共享temp_value。

然后我添加了睡眠,这是评论出来的。结果是[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;
}
}

你可以使用CallableExecutorService来做这些事情

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();
}
}

我会使用List.parallelStream()

public class Foo {
public static void main(String[] args) {
//      make initial values
List<Integer> values = new ArrayList<Integer>();
values.add(1);
values.add(2);
values.add(3);
// process each value using multiple threads.
List<Integer> results = values.parallelStream()
.map(Foo::processValue)
.collect(Collectors.toList());
}
static Integer processValue(Integer i) {
// do something interesting
return i;
}
}

即使它正在运行多线程,但我需要等待每个进程完成共享值。

正确,您需要确定。

  • 每个线程都有它唯一的数据副本。
  • 共享集合以线程安全的方式访问。

我尝试用 Threads 写这个,但在这里发布太痛苦了。

我认为您的共享数据犯了错误。 为此,请让我留在私人空间。 你可以有这样的类: 从 Foo 类中删除temp_value并将可运行部件移动到名为 PV 的新类。

Class PV implements Runnable{
int index;
Foo foo;
PV(int index,Foo foo){
this.index = index;
this.foo = foo;
}
}
@Override
public void run() {
this.foo.newValues.add(foo.values.get(index));
}
}

PV[] pvArr = new PV[max];
for (int i = 0; i < max; i++) {
pvArr[i] = new PV(i,foo);
foo.temp_value =foo.values.get(i);
thread = new Thread(pvArr[i]);
thread.start();

相关内容