java - 等到子线程完成 : Java

标签 java multithreading

问题描述:-

第 1 步:在主线程中获取用户输入的 FILE_NAME。

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

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

第 4 步:打印结果。

我做了什么:-

我用 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 t = new Thread() {
    public void run() {
        System.out.println("text");
        // other complex code
    }
 };
 t.start();
 t.join();

这样你就可以等到线程结束然后继续。您可以加入多个线程:

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

关于java - 等到子线程完成 : Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9939076/

相关文章:

python - pynput键盘监听器导致延迟

java - 同步方法中的重入

java - 使用 SSLEngine 在 NIO SSL 握手过程中抛出异常

java - 从给定数组打印偶数数组

java - install4j自动更新下载失败

java - Spring 3.1 如何将所有异常发送到一页?

c++ - 在 C++ 中,类 Java 对象监视器的最有效实现是什么?

java - 为什么在我的 JBoss Tomcat 7 + Spring MVC 项目中出现 HTTP Status 404?

c# - 如何让 C# 计时器在创建它的同一线程上执行?

java - 在库中实现同步和异步方法的正确方法是什么?