Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
297 views
in Technique[技术] by (71.8m points)

android - java start one background thread after another complete

I saw this question: how to run one thread after complete another thread , but the answer to it is not appropriate for me.

I have such kind of java code for Android:

public void startTask(Runnable r)
    {
        running = true;
        Log.i(tag, "-----------start.Runnable-----------");

        Thread first = new Thread(r);
        first.start();

        Thread second = new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                running = false;
            }
        });
}

first Thread takes as param Runnable object with some hard operation which I am processing in background service. So, when I call method: startTask() I set running = true; to prevent double executing tasks. But, also, I need right after completeness of first thread start second thread to set running = false; to enable other operations to execute.

How can I wait completeness of first thread by second not to freeze main thread?? Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You may use SingleThreadExecutor.

Executor executor = Executors.newSingleThreadExecutor();
executor.execute(runnable1);
executor.execute(runnable2);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...