• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Regarding ExecutorService from OCP (Sybex)

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,
Having doubt for the below snippet from OCP book (Page Number 336).

Confusions over below statements:
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?
2. it is possible that all three tasks were submitted for execution before the first task was even started. In this case, the single thread executor will queue the tasks and wait until the previous task completes before executing the next task.
Doubt: I didn't get it completely!!! Does that mean, first output should be "begin" only... if it is stuck then other thread won't be executed?
3. Doubt: There will be main thread and executor thread. So lets say executor thread is still running and if main thread reaches to statement "service.shutdown()" then it will stop all the threads? It means, there is possibility that all threads may not finish completely?



Thanks,
Vicky
 
Vicky Roy
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any one more doubt: Why it is checking ? Does that mean, "service" variable will be null once all thread finishes?

Thanks.
 
author
Posts: 23907
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



Single thread execution means since we are using newSingleThreadExecutor so there will be one executor thread apart than main thread. Right?

and how this question is not related to executor? now I am confused
 
Henry Wong
author
Posts: 23907
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Henry Wong
author
Posts: 23907
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vicky Roy wrote:Any one more doubt: Why it is checking ? Does that mean, "service" variable will be null once all thread finishes?



Well, first, by "all thread", I will assume you mean the executor thread only -- as the main thread is busy executing the finally block (in your question).

No. The executor do not have access to the reference, so, there is no way for it to set the variable to null upon completion.

Furthermore, there is no code in your example that sets the variable to null, so, the check is only for the case if the main thread leaves the try block before the variable is set. This can happen if a runtime exception is thrown prior to completing the instantiation of the executor.

Henry
 
Vicky Roy
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



You are right about "beging" and "end". Actually, I didn't notice that it is getting printed by SOP not by the Executor thread. So now my first question will be as below:

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
author
Posts: 23907
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Vicky Roy
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



got it. thanks.
 
Vicky Roy
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me repeat my question once again since some doubts are cleared now. The snippet below I have modified slightly.

1. (this statement from book) it is possible that all three tasks were submitted for execution before the first task was even started. In this case, the single thread executor will queue the tasks and wait until the previous task completes before executing the next task.
Doubt: I didn't get it completely!!! Does that mean, if first executor(with first for loop) get stuck then control won't move to second executor?
2. Doubt: There will be main thread and executor thread. When main thread will reach to service.shutdown() then other executor thread may be running. Now if service.shutdown() method executes then will those threads complete or shutdown() won't be called etc.?






Thanks,
Vicky
reply
    Bookmark Topic Watch Topic
  • New Topic