开发者

how to run one thread after complete another thread

开发者 https://www.devze.com 2023-02-08 02:15 出处:网络
I want to run two th开发者_StackOverflow社区reads r1, and r2. First start the r1 and after completion of r1, start r2 (only after the completion r1). How can this be done, two threads, one after anoth

I want to run two th开发者_StackOverflow社区reads r1, and r2. First start the r1 and after completion of r1, start r2 (only after the completion r1). How can this be done, two threads, one after another?


My answer here has an example using a Thread and a Handler. This method might be useful if you wanted to perform some UI updates in between the threads running.


Why don't you do all the tasks you want in one single thread? This way they will be "naturally" one after the other.

The way to implement this depends on what type of tasks you want to do and how you pass them the information they require to start.


You can use join() method for this.


  1. Make a Boolean variable with initially false
  2. Start the first thread
  3. After completion of the entire 1st thread execution make the Boolean value to true
  4. Don't start second thread util/unless the flag becomes true
0

精彩评论

暂无评论...
验证码 换一张
取 消