将值从线程传递回主线程

时间:2017-02-27 15:52:29

标签: java multithreading

我想创建一个主线程,它在完成执行后将其他(辅助)线程的值加起来。我无法将次要线程值传递给主要值。

public class RandomThread implements Runnable {

   private int rnd;

    public int getRnd() {
        return rnd;
    }

    @Override
    public void run() {

    }


    public RandomThread(int min, int max) {
        Random rand = new Random();
        int randomNumber = rand.nextInt((max - min) + 1) + min;
        this.rnd = randomNumber;
        System.out.println(rnd);

    }

public class TestClass {

    public static void main(String[] args) {

        Thread t4 = new Thread(new RandomThread(1, 10));
        try {
            t4.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t4.start();


        Thread t5 = new Thread(new RandomThread(1, 10));
        try {
            t5.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t5.start();

        Thread t6 = new Thread(new RandomThread(1, 10));
        try {
            t6.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t6.start();

        Thread t7 = new Thread(new RandomThread(1, 10));
        try {
            t7.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t7.start();

        Thread t8 = new Thread(new RandomThread(1, 10));

        try {
            t8.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t8.start();
    }

}

我需要另一个类似这样的线程。 “新线程= t4值+ t5值+ t6值+ t7值+ t8值”。

4 个答案:

答案 0 :(得分:0)

首先,如果你将所有逻辑放在RandomThread类的构造函数中计算一个随机数,你就不是在做多线程求和,而是只做一个连续求和。你应该将所有逻辑放在" run()"方法。然后,你必须打电话给" join()"在每个线程上确保程序在每个线程计算其编号之前不会退出,以便主线程可以通过" getRnd()"来检索此编号。

无论如何,您应该查看ExecutorService元素,它提供了一种简单的方法来启动线程并等待其结果。

答案 1 :(得分:0)

你犯了几个错误。

- 你应该首先启动线程:

- 然后调用.join,而不是睡觉

public class TestClass {

public static void main(String[] args) {

    Thread t4 = new Thread(new RandomThread(1, 10));
    t4.start();
    try {
        t4.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    .....
}

答案 2 :(得分:0)

  

我需要另一个类似这样的线程。 “新线程= t4值+ t5值+ t6值+ t7值+ t8值”。

正如其他人所说,如果你在线程上调用join(),这会使内存与线程同步,你可以调用getRnd()来获取随机数。

我会使用优秀的ExecutorService and associated classes并提交Callable<Integer>代替Runnable个实例。 Callable实际上会返回一个您可以检索的值。

您可以执行以下操作:

public class RandomCallable implements Callable<Integer> {
    ...
    public Integer call() {
       Random rand = new Random();
       int randomNumber = rand.nextInt((max - min) + 1) + min;
       return randomNumber;
    }
} 
...

// create a thread pool
ExecutorService threadPool = Executors.newCachedThreadPool();
List<Future<Integer>> futures = new ArrayList<>();
// submit a callable (or many) and record the future returned
futures.add(threadPool.submit(new RandomCallable(1, 10)));
// once you have submitted all of your jobs, shutdown the pool
threadPool.shutdown();
// now wait for and add up the results
int total = 0;
for (Future<Integer> future : futures) {
   total += future.get();
}

最后,正如@Vincente所提到的,你不应该在RandomThread构造函数中进行随机操作,因为它们将由主线程计算,因为它调用构造函数。此外,您的课程不是主题,应该是RandomRunnableRandomCallable

答案 3 :(得分:0)

以下是一个非常详细的例子:

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

class RandomThread implements Runnable {
  private final Random rand;
  private final int min;
  private final int max;
  private int rnd;

  public int getRnd() {
    return rnd;
  }

  @Override
  public void run() {
    final String threadName = Thread.currentThread().getName();
    System.out.println("Thread " + threadName + " started. Sleeping for 5000ms to simulate work being done...");
    try {
      Thread.sleep(5000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    final int randomNumber = rand.nextInt((max - min) + 1) + min;
    this.rnd = randomNumber;
    System.out.println("Generated [" + rnd + "] in thread " + threadName);
  }


  public RandomThread(int min, int max) {
    this.min = min;
    this.max = max;
    rand = new Random();
  }
}

class TestClass {

  private static final int NUM_THREADS = 10;

  public static void main(String[] args) {

    final List<RandomThread> runnables = new ArrayList<>();
    for (int i = 0; i < NUM_THREADS; i++) {
      runnables.add(new RandomThread(1, 10));
    }
    System.out.println("Runnables created.");

    final List<Thread> threads = new ArrayList<>();
    for (final RandomThread runnable : runnables) {
      threads.add(new Thread(runnable));
    }
    System.out.println("Threads created.");

    for (final Thread thread : threads) {
      thread.start();
    }
    System.out.println("Threads started.");

    for (final Thread thread : threads) {
      try {
        thread.join();
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
    System.out.println("Threads stopped (complete).");

    int total = 0;
    for (final RandomThread runnable : runnables) {
      total += runnable.getRnd();
    }
    System.out.println("Total: " + total);
  }
}