2

Here is what I want to do:

There are many threads start at any time, and each thread will run about 5s. When one thread is running, others must wait. And when the running thread ends, the newest thread start to run and other waiting threads just stop. Of course, there will be situations: when one thread start, there's no other thread.

I tried to use FutureTask, but failed. It seems too complex for me. Can anyone give me some idea?

4
  • 5
    Are you sure you need threads to sequentially execute tasks ? Commented Sep 17, 2012 at 8:09
  • Keep a stack of tasks, always pick the latest one and execute in a single thread. Commented Sep 17, 2012 at 8:11
  • 5
    "When one thread is running, others must wait" - then you have no concurrency whatsoever since at any given time only one thread will be running. It makes no sense to use threads. Just make a FIFO queue and launch the tasks one after the other. Threads are for running tasks in parallel. Commented Sep 17, 2012 at 8:11
  • 4
    It sounds like you are making the problem too complicated. Threads are for running concurrent, independent tasks. If you want only one task to be performed at once, you can use just one thread. Commented Sep 17, 2012 at 8:12

1 Answer 1

5

you might want to take a look at single-threaded executor, which will take your tasks from task queue and invoke them sequentially.

it is more convenient to use this class if you will decide later to add some concurrency

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I should use single-threaded executor. And add other logic to it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.