• Post Reply Bookmark Topic Watch Topic
  • New Topic

ScheduledExecutorService with a Linked List  RSS feed

 
Glenn Jayasuriya
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

So far I'm able to schedule a task at a specific time. So I can print a message tomorrow at 7am for example.
I've not learned about threads but ScheduledExecutorService uses Runnable.


From my understanding and observation the code in this class that implements runnable gets executed in a thread.

I have a linked list of Dates organized by date.
The idea is that once the event gets executed at the specific time, we'd go down the list to the next date and that would execute at it's time and so on.

And I think I got it to work:



I made a dummy linked list and it works fine.
Again i'm not familiar with threads so I'm worried about line 9 above: "activateAlarm();"

The full code for this method is:


My concern is that the runnable is calling a method that makes it's own runnable and so on.
Is isn't this a thread within a thread?
I'm wondering if this could be a problem, wondering if the threads never end.
But my implementation has one thread calling another, because I want when one task is over to call the next one.

Sorry about the terminology, again I know nothing about threads.

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

All the threads are managed by the ExecutorService. You are not creating threads. You are creating (adding) a task, and at the end of the task, you are creating (adding) the next task. That should work.

However... another way to do it, since you have all the tasks (and their times) in the link list, you can alternatively add them all in the beginning, instead of having the tasks schedule their successor.

Henry
 
Glenn Jayasuriya
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
All the threads are managed by the ExecutorService. You are not creating threads. You are creating (adding) a task, and at the end of the task, you are creating (adding) the next task. That should work.

However... another way to do it, since you have all the tasks (and their times) in the link list, you can alternatively add them all in the beginning, instead of having the tasks schedule their successor.

Henry


Thanks,
Here it says that ScheduledExecutorService returns a task object: "https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledExecutorService.html"
What is it a task object exactly?

Also I know there is an option to stop my fScheduler which is a ScheduledExecutorService.
i.e) fScheduler.shutdown();
I don't really want my code to stop because I plan in the future to add more times to my linked list of objects with dates.

Do i need to use the shutdown? Or is it ok not too?
Thanks!
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glenn Jayasuriya wrote:
Here it says that ScheduledExecutorService returns a task object: "https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledExecutorService.html"
What is it a task object exactly?

If you look at the API documentation of ScheduledExecutorService, you'll see that it says:
API documentation wrote:
The schedule methods create tasks with various delays and return a task object that can be used to cancel or check execution.

If you then scroll down and look at the schedule methods, you see that they return a ScheduledFuture. So, that's what is meant with a "task object".

A Future is an object that you use to get the result of a computation that is being done asynchronously (in another thread). Have you ever been to a restaurant where you order food, and then instead of waiting for the food, you get a small device which will buzz and blink lights when your food is ready, so that you can go and get it? A Future is something like that device. It's a stand-in object for the result (in this case, the food) that you want to get. The advantage is that you don't have to wait in line for your food, you can go and do other things (like finding a table in the restaurant) and it will warn you when your food is ready.

Likewise, a Future allows you to check if the asynchronous computation is done or not, or wait for the result, or even cancel the task if for some reason you want to do that.

To stop your executor service, you must indeed call shutdown() on it, otherwise threads will remain running and your program won't stop. But ofcourse you should not shutdown the executor service every time one task is finished. Do that at the end of the program, when the program is supposed to finish.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glenn Jayasuriya wrote:
I don't really want my code to stop because I plan in the future to add more times to my linked list of objects with dates.


As an FYI, your current implementation does have a flaw to support this. Since you need the previous task to schedule the next one, if the last task in the list executes before you add a new task, then the newly added task can't be scheduled.

I guess this is another argument to simply schedule the task as it is added to the list, instead of having the tasks schedule their successor.

Henry
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!