Reputation: 1407
I know that Thread and Task are in different abstraction-level.But anyway,I'm still confused that what's the relationship of them.And,by the way,I think that the Task tells how to do a job and the Thread actually excute the job according to a Task instance.Is my understanding correct?thank u^
Upvotes: 1
Views: 428
Reputation: 1144
Your understanding is correct.
We can do the analogy with workflow patterns where tasks are something that needs to be done in a process and threads are resources used to process or execute them.
Upvotes: 0
Reputation: 68992
A task is rather abstract it can be implemented as a process or a thread.
Upvotes: 0
Reputation: 4507
If by Task
you mean something like this, then the difference is that the task is used to run some thread-like code execution, but has additional properties, such as when to run it, how many times, and the option to cancel its execution, whereas a thread will just go ahead and run once immediately.
Upvotes: 0
Reputation: 340933
I assume by Task you mean Runnable
and Callable
. The relationship is simple:
Thread might be used to execute multiple tasks
might - because you don't need a separate thread to execute tasks (well, technically, everything runs inside a thread - you don't need a separate one)
multiple - thread can be reused; it can run multiple tasks from a collection like queue
Typically one thread executes one Runnable
passed to Thread
constructor or multiple Callable
s passed to ExecutorService
(wrapping thread pool in most cases).
Upvotes: 3