timeout method can't break executing in Completable.fromAction() - java

I discovered an error with method .timeout() in Completable, created by method .fromAction():
Completable.fromAction(() -> {
System.out.println("start time: " + timeString());
Thread.sleep(10_000);
})
.timeout(3, TimeUnit.SECONDS)
.onErrorComplete()
.blockingAwait();
System.out.println("after time: " + timeString());
Code inside the brackets executes 10 seconds, timeout set on 3 seconds. However, executing finishes only after 10 seconds. This is output of program:
start time: 14:55
after time: 15:05
Process finished with exit code 0
Question: why executing breaks not after 3 seconds and how to fix it?

Try subscribeOn(Schedulers.io()) after fromAction. timeout can't interrupt the main thread but it can interrupt tasks running on a Scheduler.

Related

How the use threads in groovy to iterate 0.4 million records

// this query returns 0.45 million records and stored in the list.
List<Employee> empList=result.getQuery(query);
Iterating employee list and setting property and finally calling service method to save employee object.
using sequential process method its taking lot of time because of the volume of records so I want to use threads .I am new to groovy and implemented only simple examples.
How to use threads for below logic using groovy?
for (Employee employee : empList) {
employee.setQuantity(8);
employeeService.save(employee);
}
There are frameworks to do this (gpars comes to mind) and also the java executors framework is a better abstraction than straight up threads, but if we want to keep things really primitive, you can split your list up in batches and run each batch on a separate thread by using something like:
def employeeService = new EmployeeService()
def empList = (1..400000).collect { new Employee() }
def batchSize = 10000
def workerThreads = empList.collate(batchSize).withIndex().collect { List<Employee> batch, int index ->
Thread.start("worker-thread-${index}") {
println "worker ${index} starting"
batch.each { Employee e ->
e.quantity = 8
employeeService.save(e)
}
println "worker ${index} completed"
}
}
println "main thread waiting for workers to finish"
workerThreads*.join()
println "workers finished, exiting..."
class Employee {
int quantity
}
class EmployeeService {
def save(Employee e) {
Thread.sleep(1)
}
}
which, when run, prints:
─➤ groovy solution.groovy
worker 7 starting
worker 11 starting
worker 5 starting
worker 13 starting
worker 17 starting
worker 16 starting
worker 2 starting
worker 18 starting
worker 6 starting
worker 15 starting
worker 12 starting
worker 14 starting
worker 1 starting
worker 4 starting
worker 10 starting
worker 8 starting
worker 9 starting
worker 3 starting
worker 0 starting
worker 20 starting
worker 21 starting
worker 19 starting
worker 22 starting
worker 24 starting
worker 23 starting
worker 25 starting
worker 26 starting
worker 27 starting
worker 28 starting
worker 29 starting
worker 30 starting
worker 31 starting
worker 32 starting
worker 33 starting
worker 34 starting
worker 35 starting
worker 36 starting
worker 37 starting
worker 38 starting
worker 39 starting
main thread waiting for workers to finish
worker 0 completed
worker 16 completed
worker 20 completed
worker 1 completed
worker 3 completed
worker 14 completed
worker 7 completed
worker 12 completed
worker 24 completed
worker 10 completed
worker 6 completed
worker 19 completed
worker 33 completed
worker 27 completed
worker 28 completed
worker 35 completed
worker 17 completed
worker 25 completed
worker 38 completed
worker 4 completed
worker 8 completed
worker 13 completed
worker 9 completed
worker 39 completed
worker 15 completed
worker 36 completed
worker 37 completed
worker 18 completed
worker 30 completed
worker 23 completed
worker 11 completed
worker 32 completed
worker 2 completed
worker 29 completed
worker 26 completed
worker 5 completed
worker 22 completed
worker 31 completed
worker 21 completed
worker 34 completed
workers finished, exiting...
List.collate splits the list of employees into chunks (List<Employee>) of size batchSize. withIndex is just there so that each batch also gets an index (i.e. just a number 0, 1, 2, 3...) for debuggability and tracing.
As we are starting threads, we need to wait for them to complete, the workerThreads*.join() is essentially doing the same thing as:
workerThreds.each { t -> t.join() }
but using a more concise syntax and Thread.join() is a java construct for waiting for a thread to complete.
Use the database, not Java
As commented by cfrick, in real work you would be using SQL to do a mass update of rows. In contrast, looping object by object in Java to update row by row in the database would be inordinately slow compared to a simple UPDATE… in SQL.
But for the sake of exploration, we will ignore this fact, and proceed with your Question.
Trying virtual threads with Project Loom
The correct Answer by Matias Bjarland inspired me to try similar code using the Project Loom technology coming to Java. Project Loom brings virtual threads (fibers) for faster concurrency with simpler coding.
Project Loom is still in the experimental stage, but is seeking feedback from the Java community. Special builds of early-access Java 16 with Project Loom technology built-in are available now for the Linux/Mac/Windows OSes.
My code here uses Java syntax, as I do not know Groovy.
I want to try similar code to the other Answer, creating a simple Employee with a a single member field quantity. And with an EmployeeService offering a save method that simulates writing to a database by merely sleeping a full second.
One major feature of Project Loom is that blocking a thread, and switching to work on another thread, now becomes very cheap. So many of the tricks and techniques used in writing Java code to avoid expensive blocking became unnecessary. So the batching seen in the other Answer should not be needed when using virtual threads. So the code below simply loops all half million Employee objects, and creates a new Runnable object for each one. As each of the new half-million Runnable objects are instantiated, they are submitted to an executor service.
We run this code twice, using either of two kinds of executor services. One is the conventional type using platform/kernel threads used for many years in Java before Project Loom, specifically, the executor service backed by a fixed thread pool. The other kind is the new executor service offered in Project Loom for virtual threads.
Executors.newFixedThreadPool( int countThreads )
Executors.newVirtualThreadExecutor()
Code
package work.basil.example;
import java.time.Duration;
import java.time.Instant;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class HalfMillion
{
public static void main ( String[] args )
{
HalfMillion app = new HalfMillion();
app.demo();
}
private void demo ( )
{
System.out.println( "java.runtime.version " + System.getProperty( "java.runtime.version" ) );
System.out.println( "INFO - `demo` method starting. " + Instant.now() );
// Populate data.
List < Employee > employees = IntStream.rangeClosed( 1 , 500_000 ).mapToObj( i -> new Employee() ).collect( Collectors.toList() );
// Submit task (updating field in each object) to an executor service.
long start = System.nanoTime();
EmployeeService employeeService = new EmployeeService();
try (
//ExecutorService executorService = Executors.newFixedThreadPool( 5 ) ; // 5 of 6 real cores, no hyper-threading.
ExecutorService executorService = Executors.newVirtualThreadExecutor() ;
)
{
employees
.stream()
.forEach(
employee -> {
executorService.submit(
new Runnable()
{
#Override
public void run ( )
{
employee.quantity = 8;
employeeService.save( employee );
}
}
);
}
);
}
// With Project Loom, the code blocks here until all submitted tasks have finished.
Duration duration = Duration.ofNanos( System.nanoTime() - start );
// Report.
System.out.println( "INFO - Done running demo for " + employees.size() + " employees taking " + duration + " to finish at " + Instant.now() );
}
class Employee
{
int quantity;
#Override
public String toString ( )
{
return "Employee{ " +
"quantity=" + quantity +
" }";
}
}
class EmployeeService
{
public void save ( Employee employee )
{
//System.out.println( "TRACE - An `EmployeeService` is doing `save` on an employee." );
try {Thread.sleep( Duration.ofSeconds( 1 ) );} catch ( InterruptedException e ) {e.printStackTrace();}
}
}
}
Results
I ran that code on a Mac mini (2018) with 3 GHz Intel Core i5 processor having 6 real cores and no hyper-threading, with 32 GB 2667 MHz DDR4 memory, and running macOS Mojave 10.14.6.
Using the new virtual threads of Project Loom
Using Executors.newVirtualThreadExecutor() takes under 5 seconds.
java.runtime.version 16-loom+9-316
INFO - `demo` method starting. 2020-12-21T09:20:36.273351Z
INFO - Done running demo for 500000 employees taking PT4.517136095S to finish at 2020-12-21T09:20:40.885315Z
If I enabled the println line within the save method, it took 15 seconds.
Using a fixed pool of 5 conventional platform/kernel threads
Using Executors.newFixedThreadPool( 5 ) takes … well, *much longer. Over a day instead of seconds: 27 hours.
java.runtime.version 16-loom+9-316
INFO - `demo` method starting. 2020-12-21T09:32:07.173561Z
INFO - Done running demo for 500000 employees taking PT27H58M18.930703698S to finish at 2020-12-22T13:30:28.813345Z
Conclusion
Well I’m not sure I can draw a conclusion here.
The results for the conventional thread pool make sense. Remember that each Java thread maps to a kernel thread in the host OS. If we are sleeping one second per employee object, as we saturate 5 cores there will mostly be 5 threads sleeping most of the time. This means the total duration should be at least a hundred thousand seconds.
The results for virtual threads on Project Loom are not believable. The command to sleep the current thread seems to ignored when using virtual threads. But I am not certain; perhaps my five physical cores on this Mac were able to be sleeping simultaneously about a hundred thousand threads each?
Please post criticisms if you find fault with my code or approach. I am not an expert on threading and concurrency.

Low performance of Java application running inside c4.large AWS instance

Im trying to perform calculation in two threads inside c4.large (machine with two cores) instance on AWS using Java 1.8 and Ubuntu. After adding second thread calculation slow down from 26 seconds to 34 per thread. I checked usage of cores and after adding second thread second core has 100% usage.
On local computer with two cores processor two threads don't slow down threads.
c4.large instance: Thread 0 start Thread 0 time: 26 seconds Thread 1 start
Thread 0 time: 29 seconds Thread 1 time: 34 seconds Thread
0 time: 34 seconds Thread 1 time: 34 seconds Thread 0 time: 34
seconds
How to improve below code or change configuration in system to improve performance?
import java.io.IOException;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.DoubleUnaryOperator;
import java.util.stream.DoubleStream;
public class TestCalculate {
private Random rnd = ThreadLocalRandom.current();
private DoubleStream randomPoints(long points, double a, double b) {
return rnd.doubles(points)
.limit(points)
.map(d -> a + d * (b - a));
}
public static void main(String[] args) throws SecurityException, IOException {
DoubleUnaryOperator du = x -> (x * Math.sqrt(23.35 * x * x) / Math.sqrt(34.54653324234324 * x) / Math.sqrt(213.3123)) * Math.sqrt(1992.34513213124 / x) / 88392.3 * x + 3.234324;
for (int i=0 ; i < 2; i++){
int j = i ;
new Thread(() -> {
TestCalculate test = new TestCalculate();
int x = 0;
System.out.println("Thread "+j+" start");
long start = System.currentTimeMillis();
while (x++ < 4) {
double d = test.randomPoints(500_000_000l, 2, 10).map(du).sum();
long end = (System.currentTimeMillis() - start) / 1000;
System.out.println("Thread "+j+" time: "+end+" seconds, result: "+d);
start = System.currentTimeMillis();
}
}).start();
try {
Thread.sleep(40_000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
On the Amazon instance types page you find this note:
Each vCPU is a hyperthread of an Intel Xeon core except for T2.
Since your c4.large instance has 2 vCPUs, what you are really getting is both hyperthreads of a single CPU core, not two independent cores. Given that, it's entirely expected that running two threads doesn't double the throughput since both threads are competing for resources on the same core. You saw a ~53% increase in throughput when adding the second thread, which actually means that this code is quite hyperthread-friendly, since an average speedup for the second hyperthread is usually considered to be in the 30% range.
You can reproduce this result locally, although on my Skylake CPU the hyperthreading penalty is apparently much lower. When I run a slightly modified version0 TestCalculate by restricting it to two different physical cores on my 4-core, 8-hyperthread as follows:
taskset -c 0,1 java stackoverflow.TestCalculate
I get the following results:
Thread 0 start
Thread 0: time: 2.21 seconds, result: 161774948.858291
Thread 0: time: 2.18 seconds, result: 161774943.838121
Thread 0: time: 2.18 seconds, result: 161774946.789039
Thread 1 start
Thread 1: time: 2.18 seconds, result: 161774945.535877
Thread 0: time: 2.18 seconds, result: 161774947.073892
Thread 1: time: 2.18 seconds, result: 161774937.356786
Thread 0: time: 2.18 seconds, result: 161774940.460682
Thread 1: time: 2.18 seconds, result: 161774944.699141
Thread 0: time: 2.18 seconds, result: 161774941.643486
Thread 0 stop
Thread 1: time: 2.18 seconds, result: 161774943.018521
Thread 1: time: 2.18 seconds, result: 161774941.866168
Thread 1: time: 2.18 seconds, result: 161774944.035612
Thread 1 stop
That is, there is approximately "perfect" scaling when adding a second thread, when each thread can run on a different core: the per-thread performance is the same to two decimal places.
On the other hand, when I run the process restricted to the same physical core1 like:
taskset -c 0,4 java stackoverflow.TestCalculate
I get the following results:
Thread 0 start
Thread 0: time: 2.22 seconds, result: 161774949.278913
Thread 0: time: 2.19 seconds, result: 161774932.329415
Thread 0: time: 2.18 seconds, result: 161774943.604470
Thread 1 start
Thread 0: time: 2.31 seconds, result: 161774951.630203
Thread 1: time: 2.31 seconds, result: 161774951.695466
Thread 0: time: 2.31 seconds, result: 161774939.631680
Thread 1: time: 2.31 seconds, result: 161774943.523282
Thread 0: time: 2.32 seconds, result: 161774948.153244
Thread 0 stop
Thread 1: time: 2.32 seconds, result: 161774956.985513
Thread 1: time: 2.18 seconds, result: 161774950.335522
Thread 1: time: 2.18 seconds, result: 161774941.739148
Thread 1: time: 2.18 seconds, result: 161774946.275329
Thread 1 stop
So there was a 6% slowdown when running on the same core. That means this code is very hyperthread friendly, since a 6% slowdown means you got a 94% benefit by adding hyperthreading! Skylake had several micro-architectural improvements that specifically helped hyperthreading scenarios, which perhaps explains the difference between your c4.large results (Haswell architecture) and mine. You might try on EC2 C5 instances since they use the Skylake architecture: if the drop is much smaller it would confirm this theory.
0 Modified to make the iteration time 10x shorter and to start the second thread deterministically after 3 iterations with a single thread.
1 On my box, logical CPUs 0 and 4, 1 and 5, etc, belong to the same physical core.

Why doesn't my RxJava Flowable respect backpressure when using observeOn?

I am trying to create a Flowable which emits events respecting backpressure to avoid memory issues, while running each stage of transformation in parallel for efficiency. I have created a simple test program to reason about the behavior of the different steps of my program and when events are being emitted vs. waiting on different stages.
My program is as follows:
public static void main(String[] args) throws ExecutionException, InterruptedException {
Stream<Integer> ints = IntStream.range(0, 1000).boxed().collect(Collectors.toList())
.stream().map(i -> {
System.out.println("emitting:" + i);
return i;
});
Flowable<Integer> flowable = Flowable.fromIterable(() -> ints.iterator());
System.out.println(String.format("Buffer size: %d", flowable.bufferSize()));
Long count = flowable.onBackpressureBuffer(10)
.buffer(10)
.flatMap(buf -> {
System.out.println("Sleeping 500 for batch");
Thread.sleep(500);
System.out.println("Got batch of events");
return Flowable.fromIterable(buf);
}, 1)
.map(x -> x + 1)
.doOnNext(i -> {
System.out.println(String.format("Sleeping : %d", i));
Thread.sleep(100);
System.out.println(i);
})
.count()
.blockingGet();
System.out.println("count: " + count);
}
When I run this, I get output that respects backpressure as expected, where a batch of events is emmited up to the size in buffer, then they are flatmapped, and finally some action is taken where they are printed one-by-one:
Buffer size: 128
emitting:0
emitting:1
emitting:2
emitting:3
emitting:4
emitting:5
emitting:6
emitting:7
emitting:8
emitting:9
Sleeping 500 for batch
Got batch of events
Sleeping : 1
1
Sleeping : 2
2
Sleeping : 3
3
Sleeping : 4
4
Sleeping : 5
5
Sleeping : 6
6
Sleeping : 7
7
Sleeping : 8
8
Sleeping : 9
9
Sleeping : 10
10
emitting:10
emitting:11
emitting:12
emitting:13
emitting:14
emitting:15
emitting:16
emitting:17
emitting:18
emitting:19
Sleeping 500 for batch
Got batch of events
Sleeping : 11
11
Sleeping : 12
12
Sleeping : 13
However if I attempt to parallelize the different stages of operation here by adding some calls to .observeOn(Schedulers.computation()) then it seems like my program no longer respects backpressure. My code now looks like:
public static void main(String[] args) throws ExecutionException, InterruptedException {
Stream<Integer> ints = IntStream.range(0, 1000).boxed().collect(Collectors.toList())
.stream().map(i -> {
System.out.println("emitting:" + i);
return i;
});
Flowable<Integer> flowable = Flowable.fromIterable(() -> ints.iterator());
System.out.println(String.format("Buffer size: %d", flowable.bufferSize()));
Long count = flowable.onBackpressureBuffer(10)
.buffer(10)
.observeOn(Schedulers.computation())
.flatMap(buf -> {
System.out.println("Sleeping 500 for batch");
Thread.sleep(500);
System.out.println("Got batch of events");
return Flowable.fromIterable(buf);
}, 1)
.map(x -> x + 1)
.observeOn(Schedulers.computation())
.doOnNext(i -> {
System.out.println(String.format("Sleeping : %d", i));
Thread.sleep(100);
System.out.println(i);
})
.observeOn(Schedulers.computation())
.count()
.blockingGet();
System.out.println("count: " + count);
}
And my output is the following, where all of my events are emitted upfront instead of respecting the backpressure and buffers specified by the various stages of execution:
Buffer size: 128
emitting:0
emitting:1
emitting:2
emitting:3
emitting:4
emitting:5
emitting:6
emitting:7
emitting:8
emitting:9
emitting:10
Sleeping 500 for batch
emitting:11
emitting:12
... everything else is emitted here ...
emitting:998
emitting:999
Got batch of events
Sleeping 500 for batch
Sleeping : 1
1
Sleeping : 2
2
Sleeping : 3
3
Sleeping : 4
4
Sleeping : 5
Got batch of events
Sleeping 500 for batch
5
Sleeping : 6
6
Sleeping : 7
7
Sleeping : 8
8
Sleeping : 9
9
Sleeping : 10
Got batch of events
Sleeping 500 for batch
10
Sleeping : 11
11
Sleeping : 12
12
Sleeping : 13
13
Sleeping : 14
14
Sleeping : 15
Got batch of events
Sleeping 500 for batch
15
Sleeping : 16
16
Sleeping : 17
17
Sleeping : 18
18
Sleeping : 19
19
Sleeping : 20
Got batch of events
Sleeping 500 for batch
20
Sleeping : 21
21
Sleeping : 22
22
Sleeping : 23
23
Sleeping : 24
24
Sleeping : 25
Got batch of events
Sleeping 500 for batch
25
Pretend my stages of batching are calling out to external services, but that I want them to run in parallel because of latency. I also want to have control of the number of items in memory at a given time because the number of items emitted initially could be highly variable, and the stages operating on batches run much slower than the initial emission of events.
How can I have my Flowable respect backpressure across a Scheduler? Why does it seem to only disrespect backpressure when I sprinkle in calls to observeOn?
How can I have my Flowable respect backpressure across a Scheduler
Actually, applying onBackpressureBuffer makes the source above it disconnect from any backpressure applied by downstream as it is an unbounded-in operator. You don't need it because Flowable.fromIterable (and by the way, RxJava has a range operator) supports and honors backpressure.
Why does it seem to only disrespect backpressure when I sprinkle in calls to observeOn?
In the first example, there is a natural backpressure happening called call-stack blocking. RxJava is synchronous by default and most operators don't introduce asynchrony, just like none do in the first example.
observeOn introduces an asynchronous boundary thus in theory, stages can run in parallel with each other. It has a default 128 element prefetch buffer which can be adjusted via one of its overloads. In your case, however, buffer(10) will actually amplify the prefetch amount to 1280 which may still lead to the complete consumption of your 1000 element long source in one go.

RxJava scheduler always works in the same thread with sleep

I have tried to run each computation on different thread, but whatever Scheduler i used it running always on single thread.
PublishProcessor processor = PublishProcessor.create();
processor
.doOnNext(i ->System.out.println(i.toString()+" emitted on "+Thread.currentThread().getId()))
.observeOn(Schedulers.newThread()).subscribe(i -> {
System.out.println(i.toString()+" received on "+Thread.currentThread().getId());
Thread.currentThread().sleep(5000);
});
processor.onNext(2);
processor.onNext(3);
processor.onNext(4);
processor.onNext(5);
processor.onNext(6);
while (true) {}
The output would be:
2 emitted on 1
3 emitted on 1
4 emitted on 1
5 emitted on 1
6 emitted on 1
2 received on 13
3 received on 13
4 received on 13
5 received on 13
6 received on 13
Thread 13 processes the next value only after sleeping, but i want to have few separate sleeping threads in that case.
Can someone explain what I'm doing wrong, please?
.observeOn(...) makes effect by changing to the item flow to another thread but it's always the same thread.
If you want to create a new thread for every item you can do
processor
.doOnNext(i ->System.out.println(i.toString()+" emitted on "+Thread.currentThread().getId()))
.flatMap(item -> Observable.just(item)
.subscribeOn(Schedulers.newThread())) // make every item change to a new thread
.subscribe(i -> {
System.out.println(i.toString()+" received on "+Thread.currentThread().getId());
Thread.currentThread().sleep(5000);
});

Synchronous task producer/consumer using ThreadPoolExecutor

I'm trying to find a way to use a ThreadPoolExecutor in the following scenario:
I have a separate thread producing and submitting tasks on the thread pool
a task submission is synchronous and will block until the task can be started by the ThreadPoolExecutor
at any given time, only a fixed number of tasks can be executing in parallel. An unbounded number of tasks running at the same time may result in memory exhaustion.
before submitting a task, the producer thread always checks that some maximum build time has not been exceeded since the first submitted task. If it was exceeded, the thread shutdowns but any task currently running on the thread pool runs to completion before the application terminates.
when the producer thread terminates, there should be no unstarted task on the queue of the thread pool.
To give more context, I currently just submit all tasks at once and cancel all the futures returned by ExecutorService.submit after the max build time is expired. I ignore all resulting CancellationExceptions since they are expected. The problem is that the behaviour of Future.cancel(false) is odd and inadequate to my use-case:
it prevents any unstarted task to run (good)
it does not interrupt currently running tasks and let them run to completion (good)
however, it ignores any exception thrown by the currently running tasks and instead throws a CancellationException for which Exception.getCause() is null. Therefore, I can't distinguish a task which has been canceled before running from a task which has continued running after the max build time and failed with an exception ! That's unfortunate, because in this case I would like to propagate the exception and report it to some error handling mechanism.
I looked into the different blocking queues Java has to offer and found this: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/SynchronousQueue.html. That seemed ideal at first, but then looking at https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ThreadPoolExecutor.html, it does not seem to play with ThreadPoolExecutor in the way I want it to:
Direct handoffs. A good default choice for a work queue is a
SynchronousQueue that hands off tasks to threads without otherwise
holding them. Here, an attempt to queue a task will fail if no threads
are immediately available to run it, so a new thread will be
constructed. This policy avoids lockups when handling sets of requests
that might have internal dependencies. Direct handoffs generally
require unbounded maximumPoolSizes to avoid rejection of new submitted
tasks. This in turn admits the possibility of unbounded thread growth
when commands continue to arrive on average faster than they can be
processed.
What would be ideal is that the consumer (= the pool) blocks on SynchronousQueue.poll and the producer (= task producer thread) blocks on SynchronousQueue.put.
Any idea how I can implement the scenario I described without writing any complex scheduling logic (what ThreadPoolExecutor should enclose for me) ?
I Believe that you're in the right path... all you have to do is use a SynchronousQueue in conjuction of a RejectedExecutionHandler, using the following constructor ... in that way you can define a fixed max size thread pool (limiting your resources usage) and define a fallback mechanism to re schedule those task that cannot be processed (because the pool was full)... Example:
public class Experiment {
public static final long HANDLER_SLEEP_TIME = 4000;
public static final int MAX_POOL_SIZE = 1;
public static void main(String[] args) throws InterruptedException {
SynchronousQueue<Runnable> queue;
RejectedExecutionHandler handler;
ThreadPoolExecutor pool;
Runnable runA, runB;
queue = new SynchronousQueue<>();
handler = new RejectedExecutionHandler() {
#Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
try {
System.out.println("Handler invoked! Thread: " + Thread.currentThread().getName());
Thread.sleep(HANDLER_SLEEP_TIME); // this let runnableA finish
executor.submit(r); // re schedule
} catch (InterruptedException ex) {
throw new RuntimeException("Handler Exception!", ex);
}
}
};
pool = new ThreadPoolExecutor(1, MAX_POOL_SIZE, 10, TimeUnit.SECONDS, queue, handler);
runA = new Runnable() {
#Override
public void run() {
try {
Thread.sleep(3000);
System.out.println("hello, I'm runnable A");
} catch (Exception ex) {
throw new RuntimeException("RunnableA", ex);
}
}
};
runB = new Runnable() {
#Override
public void run() {
System.out.println("hello, I'm runnable B");
}
};
pool.submit(runA);
pool.submit(runB);
pool.shutdown();
}
}
NOTE: the implementation of the RejectedExecutionHandler is up to you! I just only suggest a sleep as a blocking mechanism, but hrer you can do logic more complex as ask the threadpool is it has free threads or not. If not, then sleep; if yes, then submit task again...
I found another option than the one proposed by #Carlitos Way. It consists in directly adding tasks on the queue using BlockingQueue.offer. The only reason I did not manage to make it work at first and I had to post this question is that I did not know that the default behaviour of a ThreadPoolExecutor is to start without any thread. The threads will be created lazily using a thread factory and may be deleted/repopulated depending on the core and max sizes of the pool and the number of tasks being submitted concurrently.
Since the thread creation was lazy, my attempts to block on the call to offer failed because SynchronousQueue.offer immediately exits if nobody is waiting to get an element from the queue. Conversely, SynchronousQueue.put blocks until someone asks to take an item from the queue, which will never happen if the thread pool is empty.
Therefore, the workaround is to force the thread pool to create the core threads eagerly using ThreadPoolExecutor.prestartAllCoreThreads. My problem then becomes fairly trivial. I made a simplified version of my real use-case:
import java.util.Random;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicLong;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
public class SimplifiedBuildScheduler {
private static final int MAX_POOL_SIZE = 10;
private static final Random random = new Random();
private static final AtomicLong nextTaskId = new AtomicLong(0);
public static void main(String[] args) throws InterruptedException {
SynchronousQueue<Runnable> queue = new SynchronousQueue<>();
// this is a soft requirement in my system, not a real-time guarantee. See the complete semantics in my question.
long maxBuildTimeInMillis = 50;
// this timeout must be small compared to maxBuildTimeInMillis in order to accurately match the maximum build time
long taskSubmissionTimeoutInMillis = 1;
ThreadPoolExecutor pool = new ThreadPoolExecutor(MAX_POOL_SIZE, MAX_POOL_SIZE, 0, SECONDS, queue);
pool.prestartAllCoreThreads();
Runnable nextTask = makeTask(maxBuildTimeInMillis);
long millisAtStart = System.currentTimeMillis();
while (maxBuildTimeInMillis > System.currentTimeMillis() - millisAtStart) {
boolean submitted = queue.offer(nextTask, taskSubmissionTimeoutInMillis, MILLISECONDS);
if (submitted) {
nextTask = makeTask(maxBuildTimeInMillis);
} else {
System.out.println("Task " + nextTaskId.get() + " was not submitted. " + "It will be rescheduled unless " +
"the max build time has expired");
}
}
System.out.println("Max build time has expired. Stop submitting new tasks and running existing tasks to completion");
pool.shutdown();
pool.awaitTermination(9999999, SECONDS);
}
private static Runnable makeTask(long maxBuildTimeInMillis) {
long sleepTimeInMillis = randomSleepTime(maxBuildTimeInMillis);
long taskId = nextTaskId.getAndIncrement();
return () -> {
try {
System.out.println("Task " + taskId + " sleeping for " + sleepTimeInMillis + " ms");
Thread.sleep(sleepTimeInMillis);
System.out.println("Task " + taskId + " completed !");
} catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
};
}
private static int randomSleepTime(long maxBuildTimeInMillis) {
// voluntarily make it possible that a task finishes after the max build time is expired
return 1 + random.nextInt(2 * Math.toIntExact(maxBuildTimeInMillis));
}
}
An example of output is the following:
Task 1 was not submitted. It will be rescheduled unless the max build time has expired
Task 0 sleeping for 23 ms
Task 1 sleeping for 26 ms
Task 2 sleeping for 6 ms
Task 3 sleeping for 9 ms
Task 4 sleeping for 75 ms
Task 5 sleeping for 35 ms
Task 6 sleeping for 81 ms
Task 8 was not submitted. It will be rescheduled unless the max build time has expired
Task 8 was not submitted. It will be rescheduled unless the max build time has expired
Task 7 sleeping for 86 ms
Task 8 sleeping for 47 ms
Task 9 sleeping for 40 ms
Task 11 was not submitted. It will be rescheduled unless the max build time has expired
Task 2 completed !
Task 10 sleeping for 76 ms
Task 12 was not submitted. It will be rescheduled unless the max build time has expired
Task 3 completed !
Task 11 sleeping for 31 ms
Task 13 was not submitted. It will be rescheduled unless the max build time has expired
Task 13 was not submitted. It will be rescheduled unless the max build time has expired
Task 13 was not submitted. It will be rescheduled unless the max build time has expired
Task 13 was not submitted. It will be rescheduled unless the max build time has expired
Task 13 was not submitted. It will be rescheduled unless the max build time has expired
Task 13 was not submitted. It will be rescheduled unless the max build time has expired
Task 0 completed !
Task 12 sleeping for 7 ms
Task 14 was not submitted. It will be rescheduled unless the max build time has expired
Task 14 was not submitted. It will be rescheduled unless the max build time has expired
Task 1 completed !
Task 13 sleeping for 40 ms
Task 15 was not submitted. It will be rescheduled unless the max build time has expired
Task 12 completed !
Task 14 sleeping for 93 ms
Task 16 was not submitted. It will be rescheduled unless the max build time has expired
Task 16 was not submitted. It will be rescheduled unless the max build time has expired
Task 16 was not submitted. It will be rescheduled unless the max build time has expired
Task 5 completed !
Task 15 sleeping for 20 ms
Task 17 was not submitted. It will be rescheduled unless the max build time has expired
Task 17 was not submitted. It will be rescheduled unless the max build time has expired
Task 11 completed !
Task 16 sleeping for 27 ms
Task 18 was not submitted. It will be rescheduled unless the max build time has expired
Task 18 was not submitted. It will be rescheduled unless the max build time has expired
Task 9 completed !
Task 17 sleeping for 95 ms
Task 19 was not submitted. It will be rescheduled unless the max build time has expired
Max build time has expired. Stop submitting new tasks and running existing tasks to completion
Task 8 completed !
Task 15 completed !
Task 13 completed !
Task 16 completed !
Task 4 completed !
Task 6 completed !
Task 10 completed !
Task 7 completed !
Task 14 completed !
Task 17 completed !
You'll notice, for example, that task 19 was not rescheduled because the max build time expired before the scheduler can attempt to offer it to the queue a second time. You can also see than all the ongoing tasks that started before the max build time expired do run to completion.
Note: As noted in my comments in the code, the max build time is a soft requirement, which means that it might not be met exactly, and my solution indeed allows for a task to be submitted even after the max build time is expired. This can happen if the call to offer starts just before the max build time expires and finishes after. To reduce the odds of it happening, it is important that the timeout used in the call to offer is much smaller than the max build time. In the real system, the thread pool is usually busy with no idle thread, therefore the probability of this race condition to occur is extremely small, and it has no bad consequence on the system when it does happen, since the max build time is a best effort attempt to meet an overall running time, not an exact and rigid constraint.

Categories