【问题标题】:Wait until child threads completed : Java等到子线程完成:Java
【发布时间】:2012-04-13 21:48:15
【问题描述】:

问题描述:-

第 1 步: 在主线程从用户那里获取输入 FILE_NAME。

第 2 步: 对该文件执行 10 次操作(即计数字符、计数行等),所有这 10 次操作必须在单独的线程中。这意味着必须有10个子线程。

第 3 步: 主线程等待所有子线程完成。

第四步:打印结果。

我做了什么:-

我做了一个包含 3 个线程的示例代码。 我不要你这边的文件操作码。

public class ThreadTest {
    // This is object to synchronize on.
    private static final Object waitObject = ThreadTest.class;
    // Your boolean.
    private static boolean boolValue = false;

    public final Result result = new Result();

    public static void main(String[] args) {
        final ThreadTest mytest = new ThreadTest();

        System.out.println("main started");

        new Thread(new Runnable() {

            public void run() {
                System.out.println("Inside thread");

                //Int initialiser
                new Thread(new Runnable() {

                    public void run() {
                        System.out.println("Setting integer value");
                        mytest.result.setIntValue(346635);
                        System.out.println("Integer value seted");
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }).start();

                //String initialiser
                new Thread(new Runnable() {

                    public void run() {
                        System.out.println("Setting string value");
                        mytest.result.setStringValue("Hello hi");
                        System.out.println("String value seted");
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }).start();

                //Boolean initialiser
                new Thread(new Runnable() {

                    public void run() {
                        System.out.println("Setting boolean value");
                        mytest.result.setBoolValue(true);
                        System.out.println("Boolean value seted");
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }).start();

                System.out.println("Thread is finished");

                //Notify to main thread
                synchronized (ThreadTest.waitObject) {
                    ThreadTest.boolValue = true;
                    ThreadTest.waitObject.notifyAll();
                }               
            }
        }).start();

        try {
            synchronized (ThreadTest.waitObject) {
                while (!ThreadTest.boolValue) {
                    ThreadTest.waitObject.wait();
                }
            }
        } catch (InterruptedException ie) {
            ie.printStackTrace();
        }

        System.out.println("main finished");
        System.out.println("Result is : " + mytest.result.toString());
    }
}

问题:-

我上面的代码没有给出正确的答案。我该怎么做?

替代解决方案:

CountDownLatch 类也是如此。但我不想使用那个类。

我查看了this similar solution,我只想使用 Thread 的方法。

【问题讨论】:

  • 阅读 Thread 的 .join() 方法。
  • 如果是家庭作业,下次你应该把它扔掉。

标签: java multithreading


【解决方案1】:

你可以这样做:

Thread t = new Thread() {
    public void run() {
        System.out.println("text");
        // other complex code
    }
 };
 t.start();
 t.join();

这样你将等到线程完成然后继续。可以join多线程:

for (Thread thread : threads) {
  thread.join();
}

【讨论】:

  • join() 是唯一的解决方案吗?或者通过修改我给定示例中的一些代码,我可以做到这一点?
  • 这是最好、最正确、最可取的解决方案。也许你可以用其他方式来做,但我警告你 - 处理线程是乏味的,并且尽可能多地驻留在库函数中。
  • 我试过了,但是一个线程等待另一个线程开始执行。所有线程都不会同时执行。我想启动所有线程并等待它们全部完成。
  • @GünayGültekin 你会在所有加入之前调用所有开始吗?
  • @GünayGültekin 在 for 循环中构造所有线程并调用它们的 start 方法。但是不要在这个循环中调用join - 在第一次加入后,主线程将等待第一个线程完成,然后开始第二个。在第一个 for 循环之后执行第二个循环,此时在所有线程上调用 join 可以等待它们,因为它们都已经启动了
【解决方案2】:

我建议先查看Executors 框架,然后查看CompletionService

那么你可以这样写:

ExecutorService executor = Executors.newFixedThreadPool(maxThreadsToUse);
CompletionService completion = new ExecutorCompletionService(executor);
for (each sub task) {
    completion.submit(new SomeTaskYouCreate())
}
// wait for all tasks to complete.
for (int i = 0; i < numberOfSubTasks; ++i) {
     completion.take(); // will block until the next sub task has completed.
}
executor.shutdown();

【讨论】:

  • 感谢您指定完成服务。从过去 1 天开始一​​直在寻找它 :)
  • @daveb 如果我们不使用第二个 for 循环会发生什么。如果我们实现第二个 for 循环,所有子任务将并行运行
  • 这个解决方案阻塞了主线程直到它完成。如何让它在后台执行并获得完成的结果?
【解决方案3】:

有很多方法可以解决这个问题。考虑 CountDownLatch:

import java.util.concurrent.CountDownLatch;

public class WorkerTest {
    final int NUM_JOBS = 3;
    final CountDownLatch countDownLatch = new CountDownLatch(NUM_JOBS);
    final Object mutex = new Object(); 
    int workData = 0;

    public static void main(String[] args) throws Exception {
        WorkerTest workerTest = new WorkerTest();
        workerTest.go();
        workerTest.awaitAndReportData();
    }

    private void go() {
        for (int i = 0; i < NUM_JOBS; i++) {
            final int fI = i;
            Thread t = new Thread() {
                public void run() {
                    synchronized(mutex) {
                        workData++;
                    }
                    try {
                        Thread.sleep(fI * 1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    countDownLatch.countDown();
                }
            };
            t.start();
        }
    }

    private void awaitAndReportData() throws InterruptedException {
        countDownLatch.await();
        synchronized(mutex) {
            System.out.println("All workers done. workData=" + workData);
        }
    }
}

【讨论】:

    【解决方案4】:

    您可能想从java.util.concurrent 中选择CountDownLatch。来自 JavaDocs:

    一种同步辅助,允许一个或多个线程等待直到 在其他线程中执行的一组操作完成。

    示例代码:

    import java.util.concurrent.CountDownLatch;
    
    public class Test {
        private final ChildThread[] children;
        private final CountDownLatch latch;
    
        public Test() {
            this.children = new ChildThread[4];
            this.latch = new CountDownLatch(children.length);
            children[0] = new ChildThread(latch, "Task 1");
            children[1] = new ChildThread(latch, "Task 2");
            children[2] = new ChildThread(latch, "Task 3");
            children[3] = new ChildThread(latch, "Task 4");
        }
    
        public void run() {
            startChildThreads();
            waitForChildThreadsToComplete();
        }
    
        private void startChildThreads() {
            Thread[] threads = new Thread[children.length];
    
            for (int i = 0; i < threads.length; i++) {
                ChildThread child = children[i];
                threads[i] = new Thread(child);
                threads[i].start();
            }
        }
    
        private void waitForChildThreadsToComplete() {
            try {
                latch.await();
                System.out.println("All child threads have completed.");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    
        private class ChildThread implements Runnable {
            private final String name;
            private final CountDownLatch latch;
    
            protected ChildThread(CountDownLatch latch, String name) {
                this.latch = latch;
                this.name = name;
            }
    
            @Override
            public void run() {
                try {
                    // Implementation
                    System.out.println(name + " has completed.");
                } finally {
                    latch.countDown();
                }
            }
        }
    
        public static void main(String[] args) {
            Test test = new Test();
            test.run();
        }
    }
    

    输出:

    任务 1 已完成。 任务 4 已完成。 任务 3 已完成。 任务 2 已完成。 所有子线程都已完成。

    【讨论】:

      【解决方案5】:

      在 Java 8 中,更好的方法是使用 parallelStream()

      注意:更容易看到这些后台任务到底在做什么。

      public static void main(String[] args) {
          Stream.<Runnable>of(
               () -> mytest.result.setIntValue(346635),
               () -> mytest.result.setStringValue("Hello hi"),
               () -> mytest.result.setBoolValue(true) )
               .parallel()
               .forEach(Runnable::run);
      
          System.out.println("main finished");
          System.out.println("Result is : " + mytest.result.toString());
      }
      

      我取出了调试信息和睡眠,因为它们不会改变结果。

      【讨论】:

        【解决方案6】:

        每隔 n 秒检查是否所有子线程都已死机。简单而有效的方法:

                boolean allDead=false;
                while(! allDead){
                    allDead=true;
                    for (int t = 0; t < threadCount; t++)
                        if(threads[t].isAlive())    allDead=false;
                    Thread.sleep(2000);
        
                }
        

        【讨论】:

          猜你喜欢
          • 2017-07-19
          • 2011-06-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-02-15
          相关资源
          最近更新 更多