• Post Reply Bookmark Topic Watch Topic
  • New Topic

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:

 
Master Rancher
Posts: 2658
91
  • 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 would give a normal human mental abilities to rival mine. To think it is just a tiny ad:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!