how to run one thread after complete another thread
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.
- Make a Boolean variable with initially false
- Start the first thread
- After completion of the entire 1st thread execution make the Boolean value to true
- Don't start second thread util/unless the flag becomes true
精彩评论