编程技术网

关注微信公众号,定时推送前沿、专业、深度的编程技术资料。

 找回密码
 立即注册

QQ登录

只需一步,快速开始

极客时间

在主线程继续执行的同时执行两个等待另一个的线程:Execute two threads which wait one for the other while main thread continues

Mus 线程 2022-5-11 10:57 9人围观

腾讯云服务器
在主线程继续执行的同时执行两个等待另一个的线程的处理方法

如何在首先执行线程1的地方启动两个线程,如何在线程1结束时启动线程2,而主方法线程可以继续工作而又不锁定其他两个线程?

How can I start two threads where thread1 executes first, thread2 starts when thread1 ends while the main method thread can continue its work without locking on the other two?

我尝试了join(),但是它需要从必须等待另一个的线程中调用,无法执行类似thread2.join(thread1)的操作;因此,如果我在main()内要求加入连接,那么我将有效地停止执行主线程,而不仅是线程2.

I have tried join() however it needs to be called from the thread which has to wait for the other, there's no way to do something like thread2.join(thread1); If I call for a join inside main() I therefore effectively stop execution of the main thread and not only of thread2.

因此,我尝试使用ExecutorService,但又遇到了同样的问题.

I therefore tried with ExecutorService but again same problem.

import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; public class Test { public static void main(String args[]) throws InterruptedException { System.out.println(Thread.currentThread().getName() + " is Started"); class TestThread extends Thread { String name; public TestThread(String name) { this.name = name; } @Override public void run() { try { System.out.println(this + " is Started"); Thread.sleep(2000); System.out.println(this + " is Completed"); } catch (InterruptedException ex) { ex.printStackTrace(); } } @Override public String toString() { return "Thread " + name; } } ExecutorService executor = Executors.newCachedThreadPool(); executor.execute(new TestThread("1")); boolean finished = executor.awaitTermination(1, TimeUnit.HOURS); if(finished) { //I should execute thread 2 only after thread 1 has finished executor.execute(new TestThread("2")); } //I should arrive here while process 1 and 2 go on with their execution System.out.println("Hello"); } } 

#为什么我需要这样做:

我需要这个,因为线程1将元素从数据库表复制到另一个数据库,线程2必须复制一个链接表,该链接表引用从线程1复制的表.因此,仅当线程1完成时,线程2才必须开始填充其链接表,否则数据库会给出完整性错误.现在想象一下,由于复杂的链接表,我有多个线程具有不同的优先级,并且您有一个主意.

I need this because Thread1 copies elements from a database table into another database, thread2 has to copy a linking table which references the table copied from thread1. Consequently thread2 has to start populating its linking table only when thread1 has finished otherwise an integrity error is given by the database. Now imagine I have several threads with different priorities due to complex linking tables and you have an idea.

问题解答

可以像这样自定义第二个线程(将上一个线程作为参数):

The second Thread can be custom like this (takes as argument the previous thread):

public static void main(String[] a) { Thread first = new Thread(new Runnable() { @Override public void run() { } }); Thread second = new MyThread(first); first.start(); second.start(); //continue executing } public static class MyThread extends Thread { private Thread predecessor; public MyThread(Thread predecessor) { this.predecessor = predecessor; } public void run() { if (predecessor != null && predecessor.isAlive()) { try { predecessor.join(); } catch (InterruptedException e) {} } //do your stuff } } 

这篇关于在主线程继续执行的同时执行两个等待另一个的线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程技术网(www.editcode.net)!

腾讯云服务器 阿里云服务器
关注微信
^