线程并发 wait() 其他线程完成并继续其工作

发布于 2025-01-03 05:43:22 字数 422 浏览 4 评论 0原文

我想让一个线程等待另一个线程完成,而不是终止它,启动第二个线程并创建第一个线程的另一个实例。这样做是正确的吗?

Thread1 first = new Thread1();
first.start();
while(flag==true){//start while1
first.wait();
Thread2 second = new Thread2();
second.start();
while(second.isAlive()){
Toast toast = new Toast(...WORKING IN PROGRESS...);//just show something
}
flag = false;
}//end of while1

第一个线程会等到第二个线程停止运行吗?我只希望第一个线程暂停并在第二个线程完成运行后继续工作。感谢您的帮助

I want to make a thread to wait for another to finish,instead to terminate it,start the second and create another instance of the firt thread.It's right doing so?

Thread1 first = new Thread1();
first.start();
while(flag==true){//start while1
first.wait();
Thread2 second = new Thread2();
second.start();
while(second.isAlive()){
Toast toast = new Toast(...WORKING IN PROGRESS...);//just show something
}
flag = false;
}//end of while1

Will the Thread first wait untill the Thread second will stop to run?? I just want Thread first paused and continue working after thread second has finished to run. Thanks for any help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

魔法少女 2025-01-10 05:43:22

假设您正在编写 Java,则可以使用 Thread.join() 方法强制某个线程等待另一个线程结束。例如,如果您希望线程 1 等待线程 2 完成:

Thread Thread2 = new SomethingImplementingThread();
Thread Thread1 = new Thread(new Runnable() {
                  public Thread waitforthisone;
                  public void run() {
                     try {
                     waitforthisone.join();
                     } catch (InterruptedException e) {}
                     // Do stuff once waitforthisone has finished
                  });

Thread1.waitforthisone = Thread2;
Thread2.start();
Thread1.start(); // When thread 1 starts it will run Thread 2's join function, and thus 
                 // wait for thread 2 to exit

注意:代码中的 wait 命令将导致运行的线程(在本例中不是线程 1 或线程 2,而是创建它们的线程)等待() [第4行],那就去睡觉吧。具体来说,只有当另一个线程(thread1或thread2)调用first.notify()时,等待的线程才能被唤醒。否则,您上面提供的代码将永远不会超过第 4 行。

Assuming that you're writing Java, you can use the Thread.join() method to force some thread to wait for another to end. For example, If you want Thread 1 to wait until Thread 2 finishes:

Thread Thread2 = new SomethingImplementingThread();
Thread Thread1 = new Thread(new Runnable() {
                  public Thread waitforthisone;
                  public void run() {
                     try {
                     waitforthisone.join();
                     } catch (InterruptedException e) {}
                     // Do stuff once waitforthisone has finished
                  });

Thread1.waitforthisone = Thread2;
Thread2.start();
Thread1.start(); // When thread 1 starts it will run Thread 2's join function, and thus 
                 // wait for thread 2 to exit

NB: The wait command in your code will cause the thread running (neither thread1 or thread 2 in this case, but the thread that creates them) to wait() [line 4], that is go to sleep. Specifically, the thread waiting can only be woken up if another thread (thread1 or thread2) calls first.notify(). The code you've presented above will never get past line 4 otherwise.

因为看清所以看轻 2025-01-10 05:43:22

我认为您正在寻找这样的东西:

class Worker implements Runnable{

    public void run(){
        int count = 0;
        while(true){
            Toast toast = new Toast(...WORKING IN PROGRESS...);//just show something
            count++;

            if(count == 10)
                break;
        }
    }
}

然后只需将可运行的 Worker 传递到线程中:

Thread first = new Thread(new Worker());
first.start();
first.join();// waits for the first thread to finish
Thread second = new Thread(new Worker());
second.start();

另请注意,除非您创建一个名为 Worker的类Worker2,那些将不存在。然而,没有什么理由执行一个线程,等待它完成然后执行另一个线程:它完全违背了并发的目的,你还不如按顺序执行代码。

I think you're looking for something like this:

class Worker implements Runnable{

    public void run(){
        int count = 0;
        while(true){
            Toast toast = new Toast(...WORKING IN PROGRESS...);//just show something
            count++;

            if(count == 10)
                break;
        }
    }
}

Then just pass the runnable Worker into the thread:

Thread first = new Thread(new Worker());
first.start();
first.join();// waits for the first thread to finish
Thread second = new Thread(new Worker());
second.start();

Please also note that unless you make a class called Worker and Worker2, those will not exist. However, there is very little reason to execute one thread, wait for it to finish and then execute another thread: it totally defeats the purpose of concurrency and you might as well just execute the code sequentially.

仅此而已 2025-01-10 05:43:22

同步线程是完全可能的——我最喜欢的方法之一是使用 CountdownLatch ——但是您的代码显示出对如何使用它们缺乏了解。我建议在继续之前先阅读更多关于线程的知识。如果你做错了,线程和并发很容易搞砸。

It's entirely possible to synchronize threads -- one of my favorite ways is with a CountdownLatch -- but your code shows a lack of understanding of how to use them. I'd suggest reading up a bit more on Threads in general before moving forward. Threads and concurrency are very easy to screw up royally if you do it wrong.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文