From 14456eb92d1afd289ab92fe129a18bac7f442b65 Mon Sep 17 00:00:00 2001 From: Abhinab Kanrar Date: Wed, 19 Jul 2017 13:46:22 +0530 Subject: [PATCH] concurrent package description (#2265) * moving jmh into libraries module * refactoring jmh * Update pom.xml * manual algorightm * with BM result * fix for space issue * Fixed indentation * change as per suggestion * vavr either * adding unit test and othe rutilities * adding concurrent module * concurrent package description * concurrent package description * Update EitherUnitTest.java --- .../ScheduledExecutorServiceDemo.java | 46 ++++++++++++++++++ .../cyclicbarrier/CyclicBarrierExample.java | 27 +++++++++++ .../concurrent/cyclicbarrier/Task.java | 25 ++++++++++ .../concurrent/executor/ExecutorDemo.java | 17 +++++++ .../baeldung/concurrent/executor/Invoker.java | 12 +++++ .../executorservice/ExecutorServiceDemo.java | 32 +++++++++++++ .../concurrent/executorservice/Task.java | 10 ++++ .../concurrent/future/FutureDemo.java | 47 +++++++++++++++++++ .../concurrent/semaphore/SemaPhoreDemo.java | 22 +++++++++ .../threadfactory/BaeldungThreadFactory.java | 23 +++++++++ .../concurrent/threadfactory/Demo.java | 13 +++++ .../concurrent/threadfactory/Task.java | 10 ++++ 12 files changed, 284 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/concurrent/Scheduledexecutorservice/ScheduledExecutorServiceDemo.java create mode 100644 core-java/src/main/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierExample.java create mode 100644 core-java/src/main/java/com/baeldung/concurrent/cyclicbarrier/Task.java create mode 100644 core-java/src/main/java/com/baeldung/concurrent/executor/ExecutorDemo.java create mode 100644 core-java/src/main/java/com/baeldung/concurrent/executor/Invoker.java create mode 100644 core-java/src/main/java/com/baeldung/concurrent/executorservice/ExecutorServiceDemo.java create mode 100644 core-java/src/main/java/com/baeldung/concurrent/executorservice/Task.java create mode 100644 core-java/src/main/java/com/baeldung/concurrent/future/FutureDemo.java create mode 100644 core-java/src/main/java/com/baeldung/concurrent/semaphore/SemaPhoreDemo.java create mode 100644 core-java/src/main/java/com/baeldung/concurrent/threadfactory/BaeldungThreadFactory.java create mode 100644 core-java/src/main/java/com/baeldung/concurrent/threadfactory/Demo.java create mode 100644 core-java/src/main/java/com/baeldung/concurrent/threadfactory/Task.java diff --git a/core-java/src/main/java/com/baeldung/concurrent/Scheduledexecutorservice/ScheduledExecutorServiceDemo.java b/core-java/src/main/java/com/baeldung/concurrent/Scheduledexecutorservice/ScheduledExecutorServiceDemo.java new file mode 100644 index 0000000000..171f308c16 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/concurrent/Scheduledexecutorservice/ScheduledExecutorServiceDemo.java @@ -0,0 +1,46 @@ +package com.baeldung.concurrent.Scheduledexecutorservice; + +import java.util.concurrent.Callable; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; + +public class ScheduledExecutorServiceDemo { + + public void execute() { + ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(); + + ScheduledFuture scheduledFuture = executorService.schedule(new Runnable() { + @Override + public void run() { + // task details + } + }, 1, TimeUnit.SECONDS); + + executorService.scheduleAtFixedRate(new Runnable() { + @Override + public void run() { + // task details + } + }, 1, 10, TimeUnit.SECONDS); + + executorService.scheduleWithFixedDelay(new Runnable() { + @Override + public void run() { + // task details + } + }, 1, 10, TimeUnit.SECONDS); + + Future future = executorService.schedule(new Callable() { + @Override + public String call() throws Exception { + return "Hello World"; + } + }, 1, TimeUnit.SECONDS); + + executorService.shutdown(); + } + +} diff --git a/core-java/src/main/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierExample.java b/core-java/src/main/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierExample.java new file mode 100644 index 0000000000..e6075c933e --- /dev/null +++ b/core-java/src/main/java/com/baeldung/concurrent/cyclicbarrier/CyclicBarrierExample.java @@ -0,0 +1,27 @@ +package com.baeldung.concurrent.cyclicbarrier; + +import java.util.concurrent.CyclicBarrier; + +public class CyclicBarrierExample { + + public void start() { + CyclicBarrier cyclicBarrier = new CyclicBarrier(3, new Runnable() { + @Override + public void run() { + System.out.println("All previous tasks are completed"); + } + }); + + Thread t1 = new Thread(new Task(cyclicBarrier), "T1"); + Thread t2 = new Thread(new Task(cyclicBarrier), "T2"); + Thread t3 = new Thread(new Task(cyclicBarrier), "T3"); + + if (!cyclicBarrier.isBroken()) { + t1.start(); + t2.start(); + t3.start(); + } + + } + +} diff --git a/core-java/src/main/java/com/baeldung/concurrent/cyclicbarrier/Task.java b/core-java/src/main/java/com/baeldung/concurrent/cyclicbarrier/Task.java new file mode 100644 index 0000000000..4f7801e8c5 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/concurrent/cyclicbarrier/Task.java @@ -0,0 +1,25 @@ +package com.baeldung.concurrent.cyclicbarrier; + +import java.util.concurrent.BrokenBarrierException; +import java.util.concurrent.CyclicBarrier; + +public class Task implements Runnable { + + private CyclicBarrier barrier; + + public Task(CyclicBarrier barrier) { + this.barrier = barrier; + } + + @Override + public void run() { + try { + System.out.println("Thread : "+ Thread.currentThread().getName() + " is waiting"); + barrier.await(); + System.out.println("Thread : "+ Thread.currentThread().getName() + " is released"); + } catch (InterruptedException | BrokenBarrierException e) { + e.printStackTrace(); + } + } + +} diff --git a/core-java/src/main/java/com/baeldung/concurrent/executor/ExecutorDemo.java b/core-java/src/main/java/com/baeldung/concurrent/executor/ExecutorDemo.java new file mode 100644 index 0000000000..9392134bfb --- /dev/null +++ b/core-java/src/main/java/com/baeldung/concurrent/executor/ExecutorDemo.java @@ -0,0 +1,17 @@ +package com.baeldung.concurrent.executor; + +import java.util.concurrent.Executor; + +public class ExecutorDemo { + + public void execute() { + Executor executor = new Invoker(); + executor.execute(new Runnable() { + @Override + public void run() { + // task to be performed + } + }); + } + +} diff --git a/core-java/src/main/java/com/baeldung/concurrent/executor/Invoker.java b/core-java/src/main/java/com/baeldung/concurrent/executor/Invoker.java new file mode 100644 index 0000000000..d9f11986d6 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/concurrent/executor/Invoker.java @@ -0,0 +1,12 @@ +package com.baeldung.concurrent.executor; + +import java.util.concurrent.Executor; + +public class Invoker implements Executor { + + @Override + public void execute(Runnable r) { + r.run(); + } + +} diff --git a/core-java/src/main/java/com/baeldung/concurrent/executorservice/ExecutorServiceDemo.java b/core-java/src/main/java/com/baeldung/concurrent/executorservice/ExecutorServiceDemo.java new file mode 100644 index 0000000000..631ae140ab --- /dev/null +++ b/core-java/src/main/java/com/baeldung/concurrent/executorservice/ExecutorServiceDemo.java @@ -0,0 +1,32 @@ +package com.baeldung.concurrent.executorservice; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; + +public class ExecutorServiceDemo { + + ExecutorService executor = Executors.newFixedThreadPool(10); + + public void execute() { + + executor.execute(new Runnable() { + @Override + public void run() { + // task details + } + }); + + executor.submit(new Task()); + + executor.shutdown(); + executor.shutdownNow(); + try { + executor.awaitTermination(20l, TimeUnit.NANOSECONDS); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + } + +} diff --git a/core-java/src/main/java/com/baeldung/concurrent/executorservice/Task.java b/core-java/src/main/java/com/baeldung/concurrent/executorservice/Task.java new file mode 100644 index 0000000000..9a21bca80c --- /dev/null +++ b/core-java/src/main/java/com/baeldung/concurrent/executorservice/Task.java @@ -0,0 +1,10 @@ +package com.baeldung.concurrent.executorservice; + +public class Task implements Runnable { + + @Override + public void run() { + // task details + } + +} diff --git a/core-java/src/main/java/com/baeldung/concurrent/future/FutureDemo.java b/core-java/src/main/java/com/baeldung/concurrent/future/FutureDemo.java new file mode 100644 index 0000000000..89ce1a0a41 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/concurrent/future/FutureDemo.java @@ -0,0 +1,47 @@ +package com.baeldung.concurrent.future; + +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +public class FutureDemo { + + public String invoke() { + + String str = null; + + ExecutorService executorService = Executors.newFixedThreadPool(10); + + Future future = executorService.submit(new Callable() { + @Override + public String call() throws Exception { + Thread.sleep(10000l); + return "Hello World"; + } + }); + + future.cancel(false); + + try { + future.get(20, TimeUnit.SECONDS); + } catch (InterruptedException | ExecutionException | TimeoutException e1) { + e1.printStackTrace(); + } + + if (future.isDone() && !future.isCancelled()) { + try { + str = future.get(); + } catch (InterruptedException | ExecutionException e) { + e.printStackTrace(); + } + } + + return str; + + } + +} diff --git a/core-java/src/main/java/com/baeldung/concurrent/semaphore/SemaPhoreDemo.java b/core-java/src/main/java/com/baeldung/concurrent/semaphore/SemaPhoreDemo.java new file mode 100644 index 0000000000..3a1d8555d3 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/concurrent/semaphore/SemaPhoreDemo.java @@ -0,0 +1,22 @@ +package com.baeldung.concurrent.semaphore; + +import java.util.concurrent.Semaphore; + +public class SemaPhoreDemo { + + static Semaphore semaphore = new Semaphore(10); + + public void execute() throws InterruptedException { + + System.out.println("Available permit : " + semaphore.availablePermits()); + System.out.println("Number of threads waiting to acquire: " + semaphore.getQueueLength()); + + if (semaphore.tryAcquire()) { + semaphore.acquire(); + // perform some critical operations + semaphore.release(); + } + + } + +} diff --git a/core-java/src/main/java/com/baeldung/concurrent/threadfactory/BaeldungThreadFactory.java b/core-java/src/main/java/com/baeldung/concurrent/threadfactory/BaeldungThreadFactory.java new file mode 100644 index 0000000000..8744027e40 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/concurrent/threadfactory/BaeldungThreadFactory.java @@ -0,0 +1,23 @@ +package com.baeldung.concurrent.threadfactory; + +import java.util.concurrent.ThreadFactory; + +public class BaeldungThreadFactory implements ThreadFactory { + + private int threadId; + private String name; + + public BaeldungThreadFactory(String name) { + threadId = 1; + this.name = name; + } + + @Override + public Thread newThread(Runnable r) { + Thread t = new Thread(r, name + "-Thread_" + threadId); + System.out.println("created new thread with id : " + threadId + " and name : " + t.getName()); + threadId++; + return t; + } + +} diff --git a/core-java/src/main/java/com/baeldung/concurrent/threadfactory/Demo.java b/core-java/src/main/java/com/baeldung/concurrent/threadfactory/Demo.java new file mode 100644 index 0000000000..d2af97b761 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/concurrent/threadfactory/Demo.java @@ -0,0 +1,13 @@ +package com.baeldung.concurrent.threadfactory; + +public class Demo { + + public void execute() { + BaeldungThreadFactory factory = new BaeldungThreadFactory("BaeldungThreadFactory"); + for (int i = 0; i < 10; i++) { + Thread t = factory.newThread(new Task()); + t.start(); + } + } + +} diff --git a/core-java/src/main/java/com/baeldung/concurrent/threadfactory/Task.java b/core-java/src/main/java/com/baeldung/concurrent/threadfactory/Task.java new file mode 100644 index 0000000000..04ba62d457 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/concurrent/threadfactory/Task.java @@ -0,0 +1,10 @@ +package com.baeldung.concurrent.threadfactory; + +public class Task implements Runnable { + + @Override + public void run() { + // task details + } + +}