等到子线程完成:Java [英] Wait until child threads completed : Java

查看:93
本文介绍了等到子线程完成:Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题描述: -

Problem description : -

第1步:从中输入输入FILE_NAME用户在主线程。

Step 1: Take input FILE_NAME from user at main thread.

第2步:对该文件执行10次操作(即计数字符,计数行等),以及所有操作这10个操作必须是隔离线程。这意味着必须有10个子线程。

Step 2: Perform 10 operations on that file (i.e count chars, count lines etc.. ), and all those 10 operations must be in septate threads. It means there must be 10 child threads.

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

Step 3: Main thread waits until all those child threads completed.

第4步:打印结果。

我做了什么: -

What I did :-

我做了3个线程的示例代码。 我不希望您身边有文件操作代码。

I did a sample code with 3 threads. I don't want file operation code from your side.

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

问题: -

Problem :-

我的上述代码未给出正确答案。我该怎么做?

替代解决方案:

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

CountDownLatch class does the same. But I don't want to use that class.

我看了这个类似的解决方案,我想只使用Thread的方法。

I looked this similar solution and I want to use methods of Thread only.

推荐答案

你可以这样做:

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

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

This way you will wait until the thread finishes and just then continue. You can join multiple threads:

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

这篇关于等到子线程完成:Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆