• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

What is an Executor?

 
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

I'm studying Executors.

How does it decouple task submission and its execution. What is task submission and execution?

Can someone show it, with distinction between it and synchronized blocks?

Thanks
 
Saloon Keeper
Posts: 14499
325
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the olden days, you would create a thread and give that thread a task to run:

The issue with this code is that it's responsible for creating and starting threads. We're not really interested in this, we just want to execute the task without caring which thread actually runs it.

An executor is something which takes the creation and managing of threads out of our hands. When you have a class that needs to be able to execute tasks, instantiate it with an executor. In Java 8 syntax:

Now, we don't have to worry about which thread executes which task, the executor handles it all!

Executors and tasks are unrelated to synchronized blocks. Synchronized blocks prevent two different threads from executing the same piece of code at the same time.
 
Ranch Hand
Posts: 270
15
Android Angular Framework Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Stephan's comments are a great, succinct summary.

I can elaborate as to how these help me. I tend to call Executors.fixedThreadPool(int), to get an ExecutorService. An ExecutorService is a special case of Executor that allows you to (for my purposes) know when all the threads are complete, and join back to the launching thread, when it is done.

One of the best features of this, is that if you use (rather than Runnable) the Callable interface, you can return the Future<> from the "submit()" method.



I tend to keep all these futures in a collection, and when the pool has run down, I iterate over those. The "get()" method (even if it just returns a null--but that's another capability, to return a value) will then throw any exception that was thrown within the Callable's call method.



That way, you can treat the whole group of tasks you are running as "things that go into the pool", and handle exceptions re: the entire group. You can then know whether the entire, multithreaded operation had problems or not.
 
Alan Blass
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I'm still confused.

It seems the task's code is just inside the myExecutor.submit block instead of another code block which is inside the run() method in the non-executor case.

How does this separate the responsibility of creating and starting threads from executing the task?

Please advise.

Thanks.
 
L Foster
Ranch Hand
Posts: 270
15
Android Angular Framework Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Alan, Stephan's code is using a lambda. This is a sort of replacement for a boilerplate inner class. Instead of seeing



If you weren't using lambda's, you might see instead:



and this brevity is one of the drivers of the whole Lambda's thing. It began as of Java 8.
 
Alan Blass
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

I am not so concerned about Java 8.

I am wondering how does this separate the responsibility of creating and starting threads from executing the task?
 
L Foster
Ranch Hand
Posts: 270
15
Android Angular Framework Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Alan, obviously I am not explaining well, so I'll give it one more try. The lambda, which acts like a Runnable, is the task that does the work. Since it is its own runnable, it is its own object, and will do its execution in whatever thread is used. When you hand that off to the executor, the executor has the thread in which it is executed. You call the "submit" to give your task (in the lambda) to the executor. Hence, you have separation.

Good luck!
 
author
Posts: 23928
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

Since others are working on the first part, I will attempt the second part...

Alan Blass wrote:
Can someone show it, with distinction between it and synchronized blocks?



There is nothing about synchronized blocks that is specific to executors. Using synchronized blocks is a technique to deal with thread safety issues, and dealing with these issues are the same whether you use executors, the older forms of timer tasks, and/or start your own threads.

Henry


PS... L Foster, have a cow for repeatedly trying...
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alan Blass wrote:I am wondering how does this separate the responsibility of creating and starting threads from executing the task?


Because you're not involved in the business of creating all the required objects and then running them yourself. The Executor deals with all that. You can also set up an Executor to, for example, run a series of tasks one after the other, a bit like a Job scheduler, or at some time in the future - indeed, that's the precise wording of the execute() method documentation.

Basically, it removes all that control code that used to be in your program, and puts it in a reusable object, allowing you to design Executors separately from the things they run (or that run them).

HIH

Winston
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alan Blass wrote:I am not so concerned about Java 8.


I would be if I were you. Lambdas have really changed the 'roadmap' of the language (one look at Comparator will tell you that) and, while I'm still getting my head around all their possibilities myself, I can see that Java programs are likely to look very different in the future.

Winston
 
Stephan van Hulst
Saloon Keeper
Posts: 14499
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe your question boils down to: Why should we use instead of

To further clarify what Winston said, in the second piece of code, we're worrying about instantiating a new thread: Should the tread be a daemon? How many threads are already running, will an extra one really be useful? What should the priority of the thread be?

An executor handles all of this for you, and more. It's also more declarative code: It shows that all we're really interested in is executing the task, not the creation of a thread.
 
Squanch that. And squanch this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic