2016-12-07 10:34:41 -08:00
|
|
|
// lowlevel/WorkStealingPool.java
|
2016-12-30 17:23:13 -08:00
|
|
|
// (c)2017 MindView LLC: see Copyright.txt
|
2016-07-05 14:46:09 -06:00
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
2016-09-23 13:23:35 -06:00
|
|
|
// Visit http://OnJava8.com for more book information.
|
2017-01-11 16:18:57 -08:00
|
|
|
import java.util.stream.*;
|
2016-07-05 14:46:09 -06:00
|
|
|
import java.util.concurrent.*;
|
|
|
|
|
|
|
|
class ShowThread implements Runnable {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
System.out.println(
|
|
|
|
Thread.currentThread().getName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class WorkStealingPool {
|
|
|
|
public static void main(String[] args)
|
|
|
|
throws InterruptedException {
|
|
|
|
System.out.println(
|
|
|
|
Runtime.getRuntime().availableProcessors());
|
|
|
|
ExecutorService exec =
|
|
|
|
Executors.newWorkStealingPool();
|
2017-01-11 16:18:57 -08:00
|
|
|
IntStream.range(0, 10)
|
|
|
|
.mapToObj(n -> new ShowThread())
|
|
|
|
.forEach(exec::execute);
|
2016-07-05 14:46:09 -06:00
|
|
|
exec.awaitTermination(1, TimeUnit.SECONDS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Output:
|
2017-05-10 11:45:39 -06:00
|
|
|
8
|
2017-05-03 12:00:00 -06:00
|
|
|
ForkJoinPool-1-worker-2
|
2017-05-10 11:45:39 -06:00
|
|
|
ForkJoinPool-1-worker-1
|
2017-05-03 12:00:00 -06:00
|
|
|
ForkJoinPool-1-worker-2
|
2017-05-10 11:45:39 -06:00
|
|
|
ForkJoinPool-1-worker-3
|
2017-05-03 12:00:00 -06:00
|
|
|
ForkJoinPool-1-worker-2
|
2017-05-10 11:45:39 -06:00
|
|
|
ForkJoinPool-1-worker-1
|
2017-05-03 12:00:00 -06:00
|
|
|
ForkJoinPool-1-worker-3
|
|
|
|
ForkJoinPool-1-worker-1
|
2017-05-10 11:45:39 -06:00
|
|
|
ForkJoinPool-1-worker-4
|
|
|
|
ForkJoinPool-1-worker-2
|
2016-07-05 14:46:09 -06:00
|
|
|
*/
|