'Correct way to have multiple threads execute a single task
I am trying to execute a single function foo on an interval, and MULTIPLE times per interval.
The way I am currently doing it is by creating a service with a newScheduledThreadPool of whatever is specified (let's say 5 for this example). So the scheduled thread pool will be instantiated with a pool of 5.
Would this be the correct way to go about it?
Currently is executing the function 5 times at every interval (put a print statement in the function and seeing it print out 5 times every interval), but wanted to check if this is the right approach for this use case.
service = Executors.newScheduledThreadPool(threadPoolSize, runnable -> {
Thread t = new Thread(runnable, "Test");
t.setDaemon(true);
return t;
});
this.start();
}
void start() {
long initialDelay = frequencySecs + ThreadLocalRandom.current().nextInt(frequencySecs);
for (int i = 0; i < threadPoolSize; i++) {
service.scheduleAtFixedRate(this::foo, initialDelay, frequencySecs, MILLISECONDS);
}
}
Solution 1:[1]
I don't know what your function's context and purpose is cause you didn't mention enough, depending on the context, you may have some problems with multiple threads in parallel like synchronization issues. But if your context needs not synchronization you can use multiple threads that executing same function.
This implementation would be much simpler if you do not need real parallelisation:
private int scheduleCount = 0; // Global to the class
//...
private void foo(int executionCount) {
System.out.println(String.format("Schedule times %d, execution times %d", scheduleCount, i));
}
//...
Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(()-> {
scheduleCount++;
for(int i = 1; i <= 5; i++) {
foo(i);
}
}, 300, 300, TimeUnit.MILLISECONDS);
Here is the output log:
I/System.out: Schedule times 1, execution times 1
I/System.out: Schedule times 1, execution times 2
I/System.out: Schedule times 1, execution times 3
I/System.out: Schedule times 1, execution times 4
I/System.out: Schedule times 1, execution times 5
I/System.out: Schedule times 2, execution times 1
I/System.out: Schedule times 2, execution times 2
I/System.out: Schedule times 2, execution times 3
I/System.out: Schedule times 2, execution times 4
I/System.out: Schedule times 2, execution times 5
I/System.out: Schedule times 3, execution times 1
I/System.out: Schedule times 3, execution times 2
I/System.out: Schedule times 3, execution times 3
I/System.out: Schedule times 3, execution times 4
I/System.out: Schedule times 3, execution times 5
I/System.out: Schedule times 4, execution times 1
I/System.out: Schedule times 4, execution times 2
I/System.out: Schedule times 4, execution times 3
I/System.out: Schedule times 4, execution times 4
I/System.out: Schedule times 4, execution times 5
Note that you may need a handle to scheduled executor service so that you can shut it down when it is no more needed.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Kozmotronik |
