Vicky Roy wrote:
1. With a single-thread executor, results are guaranteed to be executed in the order in which they are added to the executor service.
Doubt: Does that mean the output "end" can never come before "begin"? Does that mean, at a time there will be only single thread execution may be through Round-robin scheduling?
Henry Wong wrote:
Vicky Roy wrote:
1. With a single-thread executor, results are guaranteed to be executed in the order in which they are added to the executor service.
Doubt: Does that mean the output "end" can never come before "begin"? Does that mean, at a time there will be only single thread execution may be through Round-robin scheduling?
First, please explain what you mean by "will be only single thread execution may be through Round-robin scheduling?". I am not sure what you are trying to say?
Also, I am confused by your question. A single thread executor, is simply an executor that has a single thread to execute tasks. In your example, neither "begin" or "end" are related to tasks. Hence, the question is not even related to the executor -- single threaded or otherwise.
Henry
Vicky Roy wrote:
Single thread execution means since we are using newSingleThreadExecutor so there will be one executor thread apart than main thread. Right?
Vicky Roy wrote:
and how this question is not related to executor? now I am confused
Vicky Roy wrote:
Doubt: Does that mean the output "end" can never come before "begin"?
Vicky Roy wrote:Any one more doubt: Why it is checking ? Does that mean, "service" variable will be null once all thread finishes?
Henry Wong wrote:
Vicky Roy wrote:
Single thread execution means since we are using newSingleThreadExecutor so there will be one executor thread apart than main thread. Right?
Correct.
Vicky Roy wrote:
and how this question is not related to executor? now I am confused
The output of "begin" is done by the main thread. The output of "end" is done by the main thread. Your question ...
Vicky Roy wrote:
Doubt: Does that mean the output "end" can never come before "begin"?
is asking about the order of "begin" and "end". Since neither is related to the executor thread, hence, it is not related to the executor. You can remove all the code related to the executor, and can answer your question.
Henry
Vicky Roy wrote:
1. With a single-thread executor, results are guaranteed to be executed in the order in which they are added to the executor service.
Doubt: Does that mean that the last output "Printing zoo inventory" will never come before "Printing record: <number>" ? It means it will execute all the task serially? or may be in round-robin fashion?
Henry Wong wrote:
Vicky Roy wrote:
1. With a single-thread executor, results are guaranteed to be executed in the order in which they are added to the executor service.
Doubt: Does that mean that the last output "Printing zoo inventory" will never come before "Printing record: <number>" ? It means it will execute all the task serially? or may be in round-robin fashion?
The threads in the executor simply gets a task from the work queue, executes it until completion, gets another task ... rinse, and repeat. There is no time-slicing of tasks by a thread in the executor. If you want time-slicing / scheduling, then you should instantiate an executor with more than one thread in its thread pool.
Henry