Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.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,这是我的密码 public class Main { private static class GetData implements Runnable{ private List list; private SqlQuery query; GetData(SqlQuery<String> param){ this.query=param; } public void run(){ list = quer

这是我的密码

public class Main {

private static class GetData implements Runnable{

    private List list;
    private SqlQuery query;
    GetData(SqlQuery<String> param){
        this.query=param;
    }

    public void run(){


        list = query.execute();

    }

}


public static void main(String[] args){

    ApplicationContext context = new      ClassPathXmlApplicationContext("database.xml");
    SqlQuery<String> parameter =  (SqlQuery<String>) context.getBean("BEAN_NAME");

    System.out.println("hello");


    new Thread(new Inner(parameter)).start();


   for(each element in list of inner class){

       System.out.println(element.id);
   }

}

}
公共类主{
私有静态类GetData实现可运行{
私人名单;
私有SqlQuery查询;
GetData(SqlQuery参数){
this.query=param;
}
公开募捐{
list=query.execute();
}
}
公共静态void main(字符串[]args){
ApplicationContext上下文=新的ClassPathXmlApplicationContext(“database.xml”);
SqlQuery参数=(SqlQuery)context.getBean(“BEAN_名称”);
System.out.println(“你好”);
新线程(新内部(参数)).start();
for(内部类列表中的每个元素){
System.out.println(element.id);
}
}
}
我的问题是,当我从xml文件中得到查询后,它会执行,但不会打印任何内容?为什么?


另外,如果我创建另一个线程并运行它以创建另一个列表,那么如何确保在所有线程完成执行后,只有主程序在执行中向前移动

使用要等待的线程的join方法

工作原理:

连接另一个线程(称为B)的线程(称为A)将停止执行,直到连接的线程(B)完成并返回

编辑
事实上,除非线程处于守护进程模式,否则程序不会退出。
JVM会在退出之前自动加入所有正在运行的非守护进程线程,请使用要等待的线程的方法

工作原理:

连接另一个线程(称为B)的线程(称为A)将停止执行,直到连接的线程(B)完成并返回

编辑
事实上,除非线程处于守护进程模式,否则程序不会退出。 JVM在退出新线程(runnable.start()之前自动加入所有正在运行的非守护进程线程异步执行提供的runnable,并将执行继续到下一行,因此循环在列表中添加任何内容之前执行

因此,如果希望在线程完成后执行循环,则必须等待它。最简单的方法是运行Runnable:
newinternal(参数).run()

这违背了并行执行的目的

假设您有多个runnable,您可以使用ExecutorService(而不是使用低级线程API,后者使用起来更复杂,也更容易出错)并行运行各种任务,并在所有任务完成后收集结果:

ExecutorService executor = Executors.newCachedThreadPool();
executor.submit(runnable1); //first task
executor.submit(runnable2); //second task

executor.shutdown(); //stop accepting new tasks
executor.awaiTermination(Integer.MAX_VALUE, TimeUnit.SECONDS); //wait until both tasks finish

//now you can use the results of your tasks.
最后,请注意,如果使用线程,则将在可运行线程(工作线程和主线程)中共享列表,并且需要使用线程安全结构来实现这一点,例如,通过使用:

新线程(runnable).start()
异步执行提供的runnable,并将执行继续到下一行,因此循环在列表中添加任何内容之前执行

因此,如果希望在线程完成后执行循环,则必须等待它。最简单的方法是运行Runnable:
newinternal(参数).run()

这违背了并行执行的目的

假设您有多个runnable,您可以使用ExecutorService(而不是使用低级线程API,后者使用起来更复杂,也更容易出错)并行运行各种任务,并在所有任务完成后收集结果:

ExecutorService executor = Executors.newCachedThreadPool();
executor.submit(runnable1); //first task
executor.submit(runnable2); //second task

executor.shutdown(); //stop accepting new tasks
executor.awaiTermination(Integer.MAX_VALUE, TimeUnit.SECONDS); //wait until both tasks finish

//now you can use the results of your tasks.
最后,请注意,如果使用线程,则将在可运行线程(工作线程和主线程)中共享列表,并且需要使用线程安全结构来实现这一点,例如,通过使用:

改变

并放入
t.join()之后编码>

编辑: 对于5个或任意数量的线程,表示n

像这样创建一个线程数组

Thread[] tArray = new Thread[n];

for (int j = 0; j < tArray .length; j++) {

//your code to start the thread goes here

}
Thread[]tArray=新线程[n];
对于(int j=0;j
一旦启动了它们,在主
函数的末尾再次循环它们,将它们连接到主线程

for (int j = 0; j < tarray .length; j++) {

tArray.join()

}
for(int j=0;j
更改

new Thread(new Inner(parameter)).start();

并放入
t.join()之后编码>

编辑: 对于5个或任意数量的线程,表示n

像这样创建一个线程数组

Thread[] tArray = new Thread[n];

for (int j = 0; j < tArray .length; j++) {

//your code to start the thread goes here

}
Thread[]tArray=新线程[n];
对于(int j=0;j
一旦启动了它们,在主
函数的末尾再次循环它们,将它们连接到主线程

for (int j = 0; j < tarray .length; j++) {

tArray.join()

}
for(int j=0;j
如果您使用的是一个线程,并且希望主程序等待其执行完成。您不需要使用线程机制。相反,您可以将主程序中的方法实质上添加到Thread.run()。 否则,如果要使用多线程,可以使用thread.join方法,以便所有其他线程在该行等待,直到所有线程执行完成。
我还建议您研究倒计时闩锁机制。它可以为您提供就绪机制,使您不必手动参与联接/等待操作。

如果您使用的是一个线程,并且希望主程序等待其执行完成。您不需要使用线程机制。相反,您可以将主程序中的方法实质上添加到Thread.run()。 否则,如果要使用多线程,可以使用thread.join方法,以便所有其他线程在该行等待,直到所有线程执行完成。
我还建议您研究倒计时闩锁机制。它可以为您提供一种就绪机制,使您不必手动参与加入/等待操作。

假设我必须一次创建5个线程