search

Home  >  Q&A  >  body text

spring - java异步线程执行顺序的问题

1.代码的结构简单描述如下:

 boolean flag_01 = true;
 
 for (int i = 0; i < 5; i++) {
    new Thread(new Runnable(){},"生产者").start();
    new Thread(new Runnable(){new Runnable(){//消费线程},flag_01 = false},"消费者").start();
    whiel(flag_01){}
}

请问,如何控制,当消费线程执行完之后,再执行flag_01 = false; 线程中套线程,如果获得最内层的线程,使之完成之后,再执行。。

PHPzPHPz2899 days ago419

reply all(2)I'll reply

  • 大家讲道理

    大家讲道理2017-04-18 10:04:01

    An object is passed in the consumer thread. This object contains the attribute flag_01. After the thread execution is completed, call a function of this object, set flag_01 to false, and keep reading the flag_01 attribute of this object in the outer layer to see if it is false. .

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-18 10:04:01

    I don’t understand the code you wrote.
    If you want to have a sequential execution relationship between multiple threads, you can consider java.util.concurrent.CountDownLatch
    or you have written the code completely. We'll discuss it later.

    reply
    0
  • Cancelreply