This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

ExecutorService and FutureTask implementation  RSS feed

 
Greenhorn
Posts: 23
IntelliJ IDE Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I wrote simple program to learn how ExecutorService works.
Welp, I got problem - I don't know how to get answer about thread state when it is not finished, and make shutdown at the end.
Could you help me?
Idea was:
->create callable which do whatever, but returns int at the end
-> run object of class which has ExecutorService by composition
->ask about unfinished thread to know that it is unfinished
->make stop request
->check if it is finished

Here is code, it has 3 classes:

 
author
Greenhorn
Posts: 20
6
IntelliJ IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Andrzej,

the class MyTask is missing. Instead, you posted the Main class two times. Could you update your code, please?

What I am missing:
- no shutdown of the thread pool at the end, so the three threads of the thread pool never stop and, therefore, your JVM instance never stops
- at line 13 in the Main class you call taskMachine.getNum which blocks until that task is done. So calls afterwards only operate on the completed task.

Best,
Simon
 
Andrzej Zahorski
Greenhorn
Posts: 23
IntelliJ IDE Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh sorry, I focused on making text as much readable as possible, thus I didn't noticed it, thanks.

Here it goes:

 
Rancher
Posts: 2822
96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Andrzej,

to get some information about the running tasks, I changed the 'main' method to


This 'TaskMachineOriginal' is your TaskMachine class. I only added in the constructor the number of tasks (FutureTask<Integer>), to make it a little more interesting.
As you see, I added some methods to the TaskMachine. For instance:

and

In 'magicBoxPleaseLetMeKnow' I changed the order of isDone and isCancelled, for if a task is cancelled, it is also isDone, and so it would always report 'isDone'.
in 'finish' I shutdown the ExecutorService, but that means that currently an instance of TaskMachine is one-off.

Hope this helps.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!