Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java多线程等待线程完成_Java_Multithreading - Fatal编程技术网

Java多线程等待线程完成

Java多线程等待线程完成,java,multithreading,Java,Multithreading,试图编写一个多线程java程序,但是遇到了一些问题,我的主多线程类工作得很好,但是如果我从main调用它,它会启动所有线程并转到下一个函数,我需要它在所有线程都完成之前不要继续运行。读取获取传递的文件路径readString,String Thread one = new Thread(new Runnable() { public void run() { Sys

试图编写一个多线程java程序,但是遇到了一些问题,我的主多线程类工作得很好,但是如果我从main调用它,它会启动所有线程并转到下一个函数,我需要它在所有线程都完成之前不要继续运行。读取获取传递的文件路径readString,String

           Thread one = new Thread(new Runnable() {
                 public void run()
                 {
                     System.out.println("Starting thread 1");
                     read(expiredOneYear, master1);
                     System.out.println("Finished thread 1");
                 }
            });

            Thread two = new Thread(new Runnable() {
                 public void run()
                 {
                     System.out.println("Starting thread 2");
                     read(expiredOneAndQuarterYear, master2);
                     System.out.println("Finished thread 2");
                 }
            });

            Thread three = new Thread(new Runnable() {
                 public void run()
                 {
                     System.out.println("Starting thread 3");
                     read(expiredOneAndHalfYear , master3);
                     System.out.println("Finished thread 3");
                 }
            });

            Thread four = new Thread(new Runnable() {
                 public void run()
                 {
                     System.out.println("Starting thread 4");
                     read(expiredOneAnd3QuarterYear , master4);
                     System.out.println("Finished thread 4");
                 }
            });

            // start threads
            one.start();
            two.start();
            three.start();
            four.start();
下面是主要发生的事情

CSVcompare.run(threadCount, mode, fileLocation);
CSVpattern.run(fileLocation);
在CSVcompare.run中的所有线程都完成之前,我不希望CSVpattern.run启动,否则它将无法为CSVpattern.run准备某些数据。在运行结束时添加要加入的调用。join方法等待线程完成

try
{
    one.join();
    two.join();
    three.join();
    four.join();
 }
 catch (InterruptedException e)
 {
     System.out.println("Interrupt Occurred");
     e.printStackTrace();
 }
如果你想忽略中断,也许至少应该弄清楚它为什么会被中断,但这是可行的

boolean done = false;
while (!done)
{
    try
    {
        one.join();
        two.join();
        three.join();
        four.join();
        done = true;
    }
    catch (InterruptedException e)
    {
        // Handle interrupt determine if need to exit.
    }
}

因此,添加1.run以下的内容;我这样做了,得到了一个未处理的错误异常类型InterruptedException,我正在使用Eclipsebtw@user3058423如果在等待线程完成时发生中断,welljoin可以抛出InterruptedException。将代码放入try/catch来处理它。我会编辑答案。我想第一个更新的答案是有效的,我做了一个简单的版本,剪切了代码,看看它是否有效。我现在不能做一个完整的测试,因为它实际上需要几个小时来运行数据。你真的需要一个倒计时锁。您的场景非常适合您的使用。