How to implement a fixed percentage survey using ScheduledExecutorService?

Given the following class:

public class Poller implements Runnable { public static final int CORE_POOL_SIZE = 4; public boolean running; public ScheduledExecutorService ses; public void startPolling() { this.ses = Executors.newScheduledThreadPool(CORE_POOL_SIZE); this.ses.scheduleAtFixedRate(this, 0, 1, TimeUnit.SECONDS); } public void run() { running = true; // ... Do something ... running = false; } } 

ScheduledExecutorService has a core thread pool size of 4, but will more than one poller thread be created? Since this is passed to scheduleAtFixedRate , does this mean that there will be only one thread - or is something more complicated going on behind the scenes?

And 2 bonus questions: -

  • Should running be static ?
  • Is CORE_POOL_SIZE redundant?
+4
source share
3 answers

ScheduledExecutorService has a core thread pool size of 4, but will more than one poller thread be created?

It depends - if you run your program long enough, it will probably create 4 threads. If you leave only once or twice after completing a scheduled task, you can see only 2 or 3 threads.

Why does it matter?

One way to monitor thread creation is to provide your own ThreadFactory :

 this.ses = Executors.newScheduledThreadPool(CORE_POOL_SIZE, new ThreadFactory() { @Override public Thread newThread(Runnable r) { System.out.println("Creating thread"); return new Thread(r); } }); 

Should it be static?

It depends on what you want to achieve ... Since you are not using it in your example, it's hard to say. You may need to make it static if you have multiple instances of Poller and you want them to not start at the same time, for example.

If it is static or not, if you use it as a flag, you must make it volatile to provide visibility.

Is CORE_POOL_SIZE redundant?

Not sure what you mean. This is a required parameter, so you need to specify a value. If you know for sure that two executions will not be executed at the same time, you can have only one thread. This will also prevent concurrent execution (therefore, if one scheduled task is to be started and another is already running, the new one will be delayed).

+4
source

scheduleAtFixedRate (Runnable, long initialDelay, long period, TimeUnit timeunit)

This method schedules a task to be performed periodically. The task is executed for the first time after initialDelay, and then repeated each time a period expires.

If any execution of this task throws an exception, the task is no longer executed. If no exceptions are thrown, the task will continue until the ScheduledExecutorService function is closed.

If the task takes longer than the period between its scheduled execution, the next execution starts after the completion of the current execution. A scheduled task will not be executed by more than one thread at a time.

+3
source

Why do you put your artist service in the Runnable class? You should separate the ScheduledExecutorService as a Singleton, not a runnable class variable.

Recall that this ScheduledExecutorService is a container of threads, so when you encode this

 this.ses = Executors.newScheduledThreadPool(CORE_POOL_SIZE); 

it will create many threads based on the size value at the same time when you put this code

 this.ses.scheduleAtFixedRate(this, 0, 1, TimeUnit.SECONDS); 

ScheduledExecutorService will randomly select a thread that is idle to run this class every 1 second until it finishes. if you put sleep in the run method, which goes over to the scheduled thread more than the value of the time period, it will not create another thread until the end of the 1st thread. Therefore, if you want multiple threads to run this Poller at the same time, create multiple instances of Poller and pass them to the ScheduledExecutorService

CORE_POOL_SIZE its not redundant for me, its useful to be a constant whose value is taken from the configuration file.

Should it be static? it depends on what you need. if you intend to create multiple instances of Poller then u should not

+1
source

Source: https://habr.com/ru/post/1469460/


All Articles