Java 8 Parallel Streams Examples

Few Java 8 examples to execute streams in parallel. 1. BaseStream.parallel() A simple parallel example to print 1 to 10. ParallelExample1.java package com.mkyong.java8; import java.util.stream.IntStream; public class ParallelExample1 { public static void main(String[] args) { System.out.println("Normal…"); IntStream range = IntStream.rangeClosed(1, 10); range.forEach(System.out::println); System.out.println("Parallel…"); IntStream range2 = IntStream.rangeClosed(1, 10); range2.parallel().forEach(System.out::println); } } Output Normal… 1 2 …

Read more

Java BlockingQueue examples

In Java, we can use BlockingQueue to create a queue which shared by both producer and the consumer. Producer – Generate data and put it into the queue. Consumer – Remove the data from the queue. Note Read this article to understand what is producer and consumer. The BlockingQueue implementations are thread-safe, safely be used …

Read more

Java Semaphore examples

In Java, we can use Semaphore to limit the number of threads to access a certain resource. 1. What is Semaphore? In short, a semaphore maintains a set of permits (tickets), each acquire() will take a permit (ticket) from semaphore, each release() will return back the permit (ticket) back to the semaphore. If permits (tickets) …

Read more

Java Sequence Generator examples

An example to show you how to create a thread safe sequence generator. 1. SequenceGenerator SequenceGenerator.java package com.mkyong.concurrency.examples.sequence.generator; public interface SequenceGenerator { long getNext(); } 1.1 First try, read, add, write the value directly. Below method is not thread safe, multiple threads may get the same value at the same time. UnSafeSequenceGenerator.java package com.mkyong.concurrency.examples.sequence.generator; public …

Read more

Java ExecutorService examples

In Java, we can use ExecutorService to create a thread pool, and tracks the progress of the asynchronous tasks with Future. The ExecutorService accept both Runnable and Callable tasks. Runnable – Return void, nothing. Callable – Return a Future. 1. ExecutorService 1.1 A classic ExecutorService example to create a thread pool with 5 threads, submit …

Read more

Java ScheduledExecutorService examples

In Java, we can use ScheduledExecutorService to run a task periodically or once time after a predefined delay by TimeUnit. 1. Run a Task Once The ScheduledExecutorService accepts both Runnable and Callable tasks. 1.1 Run a Runnable task after 5 seconds initial delay. ScheduledExecutorExample.java package com.mkyong.concurrency.executor.scheduler; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; public class ScheduledExecutorRunnable …

Read more

Java Fork/Join Framework examples

What is Fork/Join? Read this Java Fork/Join paper by Doug Lea The fork/join framework is available since Java 7, to make it easier to write parallel programs. We can implement the fork/join framework by extending either RecursiveTask or RecursiveAction 1. Fork/Join – RecursiveTask A fork join example to sum all the numbers from a range. …

Read more

Review : Java 7 Concurrency Cookbook

The Java 7 Concurrency Cookbook, containing over 60 examples show you how to do multithreaded programming in Java. It shows varies threading topics from beginner level to advanced level, including thread management like create, interrupt and monitor thread, using Java 5 Executor frameworks to run or schedule threads, and the latest Java 7 fork/Join Frameworks …

Read more