[BAEL-9555] - Created a core-java-modules folder
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
package com.baeldung.concurrent.Scheduledexecutorservice;
|
||||
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class ScheduledExecutorServiceDemo {
|
||||
|
||||
private void execute() {
|
||||
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
|
||||
getTasksToRun().apply(executorService);
|
||||
executorService.shutdown();
|
||||
}
|
||||
|
||||
private void executeWithMultiThread() {
|
||||
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(2);
|
||||
getTasksToRun().apply(executorService);
|
||||
executorService.shutdown();
|
||||
}
|
||||
|
||||
private Function<ScheduledExecutorService, Void> getTasksToRun() {
|
||||
return (executorService -> {
|
||||
ScheduledFuture<?> scheduledFuture1 = executorService.schedule(() -> {
|
||||
// Task
|
||||
}, 1, TimeUnit.SECONDS);
|
||||
|
||||
ScheduledFuture<?> scheduledFuture2 = executorService.scheduleAtFixedRate(() -> {
|
||||
// Task
|
||||
}, 1, 10, TimeUnit.SECONDS);
|
||||
|
||||
ScheduledFuture<?> scheduledFuture3 = executorService.scheduleWithFixedDelay(() -> {
|
||||
// Task
|
||||
}, 1, 10, TimeUnit.SECONDS);
|
||||
|
||||
ScheduledFuture<String> scheduledFuture4 = executorService.schedule(() -> {
|
||||
// Task
|
||||
return "Hellow world";
|
||||
}, 1, TimeUnit.SECONDS);
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
public static void main(String... args) {
|
||||
ScheduledExecutorServiceDemo demo = new ScheduledExecutorServiceDemo();
|
||||
demo.execute();
|
||||
demo.executeWithMultiThread();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.concurrent.callable;
|
||||
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
public class FactorialTask implements Callable<Integer> {
|
||||
int number;
|
||||
|
||||
public FactorialTask(int number) {
|
||||
this.number = number;
|
||||
}
|
||||
|
||||
public Integer call() throws InvalidParamaterException {
|
||||
int fact=1;
|
||||
if(number < 0)
|
||||
throw new InvalidParamaterException("Number must be positive");
|
||||
|
||||
for(int count=number;count>1;count--){
|
||||
fact=fact * count;
|
||||
}
|
||||
|
||||
return fact;
|
||||
}
|
||||
|
||||
private class InvalidParamaterException extends Exception {
|
||||
public InvalidParamaterException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.concurrent.cyclicbarrier;
|
||||
|
||||
import java.util.concurrent.CyclicBarrier;
|
||||
|
||||
public class CyclicBarrierExample {
|
||||
|
||||
public void start() {
|
||||
CyclicBarrier cyclicBarrier = new CyclicBarrier(3, () -> {
|
||||
// Task
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.concurrent.executor;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
public class ExecutorDemo {
|
||||
|
||||
public void execute() {
|
||||
Executor executor = new Invoker();
|
||||
executor.execute(()->{
|
||||
// task to be performed
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.baeldung.concurrent.executorservice;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
public class DelayedCallable implements Callable<String> {
|
||||
|
||||
private String name;
|
||||
private long period;
|
||||
private CountDownLatch latch;
|
||||
|
||||
public DelayedCallable(String name, long period, CountDownLatch latch) {
|
||||
this(name, period);
|
||||
this.latch = latch;
|
||||
}
|
||||
|
||||
public DelayedCallable(String name, long period) {
|
||||
this.name = name;
|
||||
this.period = period;
|
||||
}
|
||||
|
||||
public String call() {
|
||||
|
||||
try {
|
||||
Thread.sleep(period);
|
||||
|
||||
if (latch != null) {
|
||||
latch.countDown();
|
||||
}
|
||||
|
||||
} catch (InterruptedException ex) {
|
||||
// handle exception
|
||||
ex.printStackTrace();
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
|
||||
return name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
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.submit(() -> {
|
||||
new Task();
|
||||
});
|
||||
|
||||
executor.shutdown();
|
||||
executor.shutdownNow();
|
||||
try {
|
||||
executor.awaitTermination(20l, TimeUnit.NANOSECONDS);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.concurrent.executorservice;
|
||||
|
||||
public class Task implements Runnable {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
// task details
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.concurrent.future;
|
||||
|
||||
import java.util.concurrent.RecursiveTask;
|
||||
|
||||
public class FactorialSquareCalculator extends RecursiveTask<Integer> {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
final private Integer n;
|
||||
|
||||
FactorialSquareCalculator(Integer n) {
|
||||
this.n = n;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Integer compute() {
|
||||
if (n <= 1) {
|
||||
return n;
|
||||
}
|
||||
|
||||
FactorialSquareCalculator calculator = new FactorialSquareCalculator(n - 1);
|
||||
|
||||
calculator.fork();
|
||||
|
||||
return n * n + calculator.join();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.baeldung.concurrent.future;
|
||||
|
||||
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<String> future = executorService.submit(() -> {
|
||||
// Task
|
||||
Thread.sleep(10000l);
|
||||
return "Hellow 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;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.concurrent.future;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
class SquareCalculator {
|
||||
|
||||
private final ExecutorService executor;
|
||||
|
||||
SquareCalculator(ExecutorService executor) {
|
||||
this.executor = executor;
|
||||
}
|
||||
|
||||
Future<Integer> calculate(Integer input) {
|
||||
return executor.submit(() -> {
|
||||
Thread.sleep(1000);
|
||||
return input * input;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.concurrent.runnable;
|
||||
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class EventLoggingTask implements Runnable{
|
||||
private Logger logger = LoggerFactory.getLogger(EventLoggingTask.class);
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
String messge="Message read from the event queue";
|
||||
logger.info("Message read from event queue is "+messge);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.concurrent.runnable;
|
||||
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
public class TaskRunner {
|
||||
|
||||
private static ExecutorService executorService;
|
||||
|
||||
public static void main(String[] args) {
|
||||
executeTask();
|
||||
}
|
||||
|
||||
private static void executeTask() {
|
||||
executorService= Executors.newSingleThreadExecutor();
|
||||
|
||||
EventLoggingTask task = new EventLoggingTask();
|
||||
|
||||
Future future = executorService.submit(task);
|
||||
|
||||
executorService.shutdown();
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.concurrent.sleepwait;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/***
|
||||
* Example of waking up a waiting thread
|
||||
*/
|
||||
public class ThreadA {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(ThreadA.class);
|
||||
|
||||
private static final ThreadB b = new ThreadB();
|
||||
|
||||
public static void main(String... args) throws InterruptedException {
|
||||
b.start();
|
||||
|
||||
synchronized (b) {
|
||||
while (b.sum == 0) {
|
||||
LOG.debug("Waiting for ThreadB to complete...");
|
||||
b.wait();
|
||||
}
|
||||
|
||||
LOG.debug("ThreadB has completed. Sum from that thread is: " + b.sum);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.concurrent.sleepwait;
|
||||
|
||||
/***
|
||||
* Example of waking up a waiting thread
|
||||
*/
|
||||
class ThreadB extends Thread {
|
||||
int sum;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
synchronized (this) {
|
||||
int i = 0;
|
||||
while (i < 100000) {
|
||||
sum += i;
|
||||
i++;
|
||||
}
|
||||
notify();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.concurrent.sleepwait;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/***
|
||||
* Example of wait() and sleep() methods
|
||||
*/
|
||||
public class WaitSleepExample {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(WaitSleepExample.class);
|
||||
|
||||
private static final Object LOCK = new Object();
|
||||
|
||||
public static void main(String... args) throws InterruptedException {
|
||||
sleepWaitInSynchronizedBlocks();
|
||||
}
|
||||
|
||||
private static void sleepWaitInSynchronizedBlocks() throws InterruptedException {
|
||||
Thread.sleep(1000); // called on the thread
|
||||
LOG.debug("Thread '" + Thread.currentThread().getName() + "' is woken after sleeping for 1 second");
|
||||
|
||||
synchronized (LOCK) {
|
||||
LOCK.wait(1000); // called on the object, synchronization required
|
||||
LOG.debug("Object '" + LOCK + "' is woken after waiting for 1 second");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.baeldung.concurrent.stopping;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
public class ControlSubThread implements Runnable {
|
||||
|
||||
private Thread worker;
|
||||
private int interval = 100;
|
||||
private AtomicBoolean running = new AtomicBoolean(false);
|
||||
private AtomicBoolean stopped = new AtomicBoolean(true);
|
||||
|
||||
|
||||
public ControlSubThread(int sleepInterval) {
|
||||
interval = sleepInterval;
|
||||
}
|
||||
|
||||
public void start() {
|
||||
worker = new Thread(this);
|
||||
worker.start();
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
running.set(false);
|
||||
}
|
||||
|
||||
public void interrupt() {
|
||||
running.set(false);
|
||||
worker.interrupt();
|
||||
}
|
||||
|
||||
boolean isRunning() {
|
||||
return running.get();
|
||||
}
|
||||
|
||||
boolean isStopped() {
|
||||
return stopped.get();
|
||||
}
|
||||
|
||||
public void run() {
|
||||
running.set(true);
|
||||
stopped.set(false);
|
||||
while (running.get()) {
|
||||
try {
|
||||
Thread.sleep(interval);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
System.out.println("Thread was interrupted, Failed to complete operation");
|
||||
}
|
||||
// do something
|
||||
}
|
||||
stopped.set(true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.baeldung.concurrent.synchronize;
|
||||
|
||||
public class BaeldungSynchronizedBlocks {
|
||||
|
||||
private int count = 0;
|
||||
private static int staticCount = 0;
|
||||
|
||||
void performSynchronisedTask() {
|
||||
synchronized (this) {
|
||||
setCount(getCount() + 1);
|
||||
}
|
||||
}
|
||||
|
||||
static void performStaticSyncTask() {
|
||||
synchronized (BaeldungSynchronizedBlocks.class) {
|
||||
setStaticCount(getStaticCount() + 1);
|
||||
}
|
||||
}
|
||||
|
||||
public int getCount() {
|
||||
return count;
|
||||
}
|
||||
|
||||
public void setCount(int count) {
|
||||
this.count = count;
|
||||
}
|
||||
|
||||
static int getStaticCount() {
|
||||
return staticCount;
|
||||
}
|
||||
|
||||
private static void setStaticCount(int staticCount) {
|
||||
BaeldungSynchronizedBlocks.staticCount = staticCount;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.baeldung.concurrent.synchronize;
|
||||
|
||||
public class BaeldungSynchronizedMethods {
|
||||
|
||||
private int sum = 0;
|
||||
private int syncSum = 0;
|
||||
|
||||
static int staticSum = 0;
|
||||
|
||||
void calculate() {
|
||||
setSum(getSum() + 1);
|
||||
}
|
||||
|
||||
synchronized void synchronisedCalculate() {
|
||||
setSyncSum(getSyncSum() + 1);
|
||||
}
|
||||
|
||||
static synchronized void syncStaticCalculate() {
|
||||
staticSum = staticSum + 1;
|
||||
}
|
||||
|
||||
public int getSum() {
|
||||
return sum;
|
||||
}
|
||||
|
||||
public void setSum(int sum) {
|
||||
this.sum = sum;
|
||||
}
|
||||
|
||||
int getSyncSum() {
|
||||
return syncSum;
|
||||
}
|
||||
|
||||
private void setSyncSum(int syncSum) {
|
||||
this.syncSum = syncSum;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.concurrent.threadfactory;
|
||||
|
||||
public class Task implements Runnable {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
// task details
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.concurrent.threadlifecycle;
|
||||
|
||||
public class BlockedState {
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
Thread t1 = new Thread(new DemoThreadB());
|
||||
Thread t2 = new Thread(new DemoThreadB());
|
||||
|
||||
t1.start();
|
||||
t2.start();
|
||||
|
||||
Thread.sleep(1000);
|
||||
|
||||
System.out.println(t2.getState());
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
class DemoThreadB implements Runnable {
|
||||
@Override
|
||||
public void run() {
|
||||
commonResource();
|
||||
}
|
||||
|
||||
public static synchronized void commonResource() {
|
||||
while(true) {
|
||||
// Infinite loop to mimic heavy processing
|
||||
// Thread 't1' won't leave this method
|
||||
// when Thread 't2' enters this
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.concurrent.threadlifecycle;
|
||||
|
||||
public class NewState implements Runnable {
|
||||
public static void main(String[] args) {
|
||||
Runnable runnable = new NewState();
|
||||
Thread t = new Thread(runnable);
|
||||
System.out.println(t.getState());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.concurrent.threadlifecycle;
|
||||
|
||||
public class RunnableState implements Runnable {
|
||||
public static void main(String[] args) {
|
||||
Runnable runnable = new NewState();
|
||||
Thread t = new Thread(runnable);
|
||||
t.start();
|
||||
System.out.println(t.getState());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.concurrent.threadlifecycle;
|
||||
|
||||
public class TerminatedState implements Runnable {
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
Thread t1 = new Thread(new TerminatedState());
|
||||
t1.start();
|
||||
Thread.sleep(1000);
|
||||
System.out.println(t1.getState());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
// No processing in this block
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.concurrent.threadlifecycle;
|
||||
|
||||
public class TimedWaitingState {
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
DemoThread obj1 = new DemoThread();
|
||||
Thread t1 = new Thread(obj1);
|
||||
t1.start();
|
||||
// The following sleep will give enough time for ThreadScheduler
|
||||
// to start processing of thread t1
|
||||
Thread.sleep(1000);
|
||||
System.out.println(t1.getState());
|
||||
}
|
||||
}
|
||||
|
||||
class DemoThread implements Runnable {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
Thread.sleep(5000);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.baeldung.concurrent.threadlifecycle;
|
||||
|
||||
public class WaitingState implements Runnable {
|
||||
public static Thread t1;
|
||||
|
||||
public static void main(String[] args) {
|
||||
t1 = new Thread(new WaitingState());
|
||||
t1.start();
|
||||
}
|
||||
|
||||
public void run() {
|
||||
Thread t2 = new Thread(new DemoThreadWS());
|
||||
t2.start();
|
||||
|
||||
try {
|
||||
t2.join();
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class DemoThreadWS implements Runnable {
|
||||
public void run() {
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
System.out.println(WaitingState.t1.getState());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package com.baeldung.concurrent.threadsafety.application;
|
||||
|
||||
import com.baeldung.concurrent.threadsafety.callables.AtomicCounterCallable;
|
||||
import com.baeldung.concurrent.threadsafety.mathutils.MathUtils;
|
||||
import com.baeldung.concurrent.threadsafety.callables.CounterCallable;
|
||||
import com.baeldung.concurrent.threadsafety.callables.ExtrinsicLockCounterCallable;
|
||||
import com.baeldung.concurrent.threadsafety.callables.MessageServiceCallable;
|
||||
import com.baeldung.concurrent.threadsafety.callables.ReentranReadWriteLockCounterCallable;
|
||||
import com.baeldung.concurrent.threadsafety.callables.ReentrantLockCounterCallable;
|
||||
import com.baeldung.concurrent.threadsafety.services.AtomicCounter;
|
||||
import com.baeldung.concurrent.threadsafety.services.Counter;
|
||||
import com.baeldung.concurrent.threadsafety.services.ExtrinsicLockCounter;
|
||||
import com.baeldung.concurrent.threadsafety.services.MessageService;
|
||||
import com.baeldung.concurrent.threadsafety.services.ReentrantLockCounter;
|
||||
import com.baeldung.concurrent.threadsafety.services.ReentrantReadWriteLockCounter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) throws InterruptedException, ExecutionException {
|
||||
|
||||
new Thread(() -> {
|
||||
System.out.println(MathUtils.factorial(10));
|
||||
}).start();
|
||||
new Thread(() -> {
|
||||
System.out.println(MathUtils.factorial(5));
|
||||
}).start();
|
||||
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(10);
|
||||
MessageService messageService = new MessageService("Welcome to Baeldung!");
|
||||
Future<String> future1 = (Future<String>) executorService.submit(new MessageServiceCallable(messageService));
|
||||
Future<String> future2 = (Future<String>) executorService.submit(new MessageServiceCallable(messageService));
|
||||
System.out.println(future1.get());
|
||||
System.out.println(future2.get());
|
||||
|
||||
Counter counter = new Counter();
|
||||
Future<Integer> future3 = (Future<Integer>) executorService.submit(new CounterCallable(counter));
|
||||
Future<Integer> future4 = (Future<Integer>) executorService.submit(new CounterCallable(counter));
|
||||
System.out.println(future3.get());
|
||||
System.out.println(future4.get());
|
||||
|
||||
ExtrinsicLockCounter extrinsicLockCounter = new ExtrinsicLockCounter();
|
||||
Future<Integer> future5 = (Future<Integer>) executorService.submit(new ExtrinsicLockCounterCallable(extrinsicLockCounter));
|
||||
Future<Integer> future6 = (Future<Integer>) executorService.submit(new ExtrinsicLockCounterCallable(extrinsicLockCounter));
|
||||
System.out.println(future5.get());
|
||||
System.out.println(future6.get());
|
||||
|
||||
ReentrantLockCounter reentrantLockCounter = new ReentrantLockCounter();
|
||||
Future<Integer> future7 = (Future<Integer>) executorService.submit(new ReentrantLockCounterCallable(reentrantLockCounter));
|
||||
Future<Integer> future8 = (Future<Integer>) executorService.submit(new ReentrantLockCounterCallable(reentrantLockCounter));
|
||||
System.out.println(future7.get());
|
||||
System.out.println(future8.get());
|
||||
|
||||
ReentrantReadWriteLockCounter reentrantReadWriteLockCounter = new ReentrantReadWriteLockCounter();
|
||||
Future<Integer> future9 = (Future<Integer>) executorService.submit(new ReentranReadWriteLockCounterCallable(reentrantReadWriteLockCounter));
|
||||
Future<Integer> future10 = (Future<Integer>) executorService.submit(new ReentranReadWriteLockCounterCallable(reentrantReadWriteLockCounter));
|
||||
System.out.println(future9.get());
|
||||
System.out.println(future10.get());
|
||||
|
||||
AtomicCounter atomicCounter = new AtomicCounter();
|
||||
Future<Integer> future11 = (Future<Integer>) executorService.submit(new AtomicCounterCallable(atomicCounter));
|
||||
Future<Integer> future12 = (Future<Integer>) executorService.submit(new AtomicCounterCallable(atomicCounter));
|
||||
System.out.println(future11.get());
|
||||
System.out.println(future12.get());
|
||||
|
||||
Collection<Integer> syncCollection = Collections.synchronizedCollection(new ArrayList<>());
|
||||
Thread thread11 = new Thread(() -> syncCollection.addAll(Arrays.asList(1, 2, 3, 4, 5, 6)));
|
||||
Thread thread12 = new Thread(() -> syncCollection.addAll(Arrays.asList(1, 2, 3, 4, 5, 6)));
|
||||
thread11.start();
|
||||
thread12.start();
|
||||
|
||||
Map<String,String> concurrentMap = new ConcurrentHashMap<>();
|
||||
concurrentMap.put("1", "one");
|
||||
concurrentMap.put("2", "two");
|
||||
concurrentMap.put("3", "three");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.concurrent.threadsafety.callables;
|
||||
|
||||
import com.baeldung.concurrent.threadsafety.services.AtomicCounter;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
public class AtomicCounterCallable implements Callable<Integer> {
|
||||
|
||||
private final AtomicCounter counter;
|
||||
|
||||
public AtomicCounterCallable(AtomicCounter counter) {
|
||||
this.counter = counter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer call() throws Exception {
|
||||
counter.incrementCounter();
|
||||
return counter.getCounter();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.concurrent.threadsafety.callables;
|
||||
|
||||
import com.baeldung.concurrent.threadsafety.services.Counter;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
public class CounterCallable implements Callable<Integer> {
|
||||
|
||||
private final Counter counter;
|
||||
|
||||
public CounterCallable(Counter counter) {
|
||||
this.counter = counter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer call() throws Exception {
|
||||
counter.incrementCounter();
|
||||
return counter.getCounter();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.concurrent.threadsafety.callables;
|
||||
|
||||
import com.baeldung.concurrent.threadsafety.services.ExtrinsicLockCounter;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
public class ExtrinsicLockCounterCallable implements Callable<Integer> {
|
||||
|
||||
private final ExtrinsicLockCounter counter;
|
||||
|
||||
public ExtrinsicLockCounterCallable(ExtrinsicLockCounter counter) {
|
||||
this.counter = counter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer call() throws Exception {
|
||||
counter.incrementCounter();
|
||||
return counter.getCounter();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.concurrent.threadsafety.callables;
|
||||
|
||||
import com.baeldung.concurrent.threadsafety.services.MessageService;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
public class MessageServiceCallable implements Callable<String> {
|
||||
|
||||
private final MessageService messageService;
|
||||
|
||||
public MessageServiceCallable(MessageService messageService) {
|
||||
this.messageService = messageService;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String call() {
|
||||
return messageService.getMesssage();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.concurrent.threadsafety.callables;
|
||||
|
||||
import com.baeldung.concurrent.threadsafety.services.ReentrantReadWriteLockCounter;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
public class ReentranReadWriteLockCounterCallable implements Callable<Integer> {
|
||||
|
||||
private final ReentrantReadWriteLockCounter counter;
|
||||
|
||||
public ReentranReadWriteLockCounterCallable(ReentrantReadWriteLockCounter counter) {
|
||||
this.counter = counter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer call() throws Exception {
|
||||
counter.incrementCounter();
|
||||
return counter.getCounter();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.concurrent.threadsafety.callables;
|
||||
|
||||
import com.baeldung.concurrent.threadsafety.services.ReentrantLockCounter;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
public class ReentrantLockCounterCallable implements Callable<Integer> {
|
||||
|
||||
private final ReentrantLockCounter counter;
|
||||
|
||||
public ReentrantLockCounterCallable(ReentrantLockCounter counter) {
|
||||
this.counter = counter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer call() throws Exception {
|
||||
counter.incrementCounter();
|
||||
return counter.getCounter();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.concurrent.threadsafety.mathutils;
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
public class MathUtils {
|
||||
|
||||
public static BigInteger factorial(int number) {
|
||||
BigInteger f = new BigInteger("1");
|
||||
for (int i = 2; i <= number; i++) {
|
||||
f = f.multiply(BigInteger.valueOf(i));
|
||||
}
|
||||
return f;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.concurrent.threadsafety.services;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class AtomicCounter {
|
||||
|
||||
private final AtomicInteger counter = new AtomicInteger();
|
||||
|
||||
public AtomicCounter() {}
|
||||
|
||||
public void incrementCounter() {
|
||||
counter.incrementAndGet();
|
||||
}
|
||||
|
||||
public synchronized int getCounter() {
|
||||
return counter.get();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.concurrent.threadsafety.services;
|
||||
|
||||
public class Counter {
|
||||
|
||||
private volatile int counter;
|
||||
|
||||
public Counter() {
|
||||
this.counter = 0;
|
||||
}
|
||||
|
||||
public synchronized void incrementCounter() {
|
||||
counter += 1;
|
||||
}
|
||||
|
||||
public int getCounter() {
|
||||
return counter;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.concurrent.threadsafety.services;
|
||||
|
||||
public class ExtrinsicLockCounter {
|
||||
|
||||
private int counter;
|
||||
private final Object lock = new Object();
|
||||
|
||||
public ExtrinsicLockCounter() {
|
||||
this.counter = 0;
|
||||
}
|
||||
|
||||
public void incrementCounter() {
|
||||
synchronized (lock) {
|
||||
counter += 1;
|
||||
}
|
||||
}
|
||||
|
||||
public int getCounter() {
|
||||
synchronized (lock) {
|
||||
return counter;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.concurrent.threadsafety.services;
|
||||
|
||||
public class MessageService {
|
||||
|
||||
private final String message;
|
||||
|
||||
public MessageService(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public String getMesssage() {
|
||||
return message;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.concurrent.threadsafety.services;
|
||||
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
public class ReentrantLockCounter {
|
||||
|
||||
private int counter;
|
||||
private final ReentrantLock reLock = new ReentrantLock(true);
|
||||
|
||||
public ReentrantLockCounter() {
|
||||
this.counter = 0;
|
||||
}
|
||||
|
||||
public void incrementCounter() {
|
||||
reLock.lock();
|
||||
try {
|
||||
counter += 1;
|
||||
} finally {
|
||||
reLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public int getCounter() {
|
||||
return counter;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.concurrent.threadsafety.services;
|
||||
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
|
||||
public class ReentrantReadWriteLockCounter {
|
||||
|
||||
private int counter;
|
||||
private final ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();
|
||||
private final Lock readLock = rwLock.readLock();
|
||||
private final Lock writeLock = rwLock.writeLock();
|
||||
|
||||
public ReentrantReadWriteLockCounter() {
|
||||
this.counter = 0;
|
||||
}
|
||||
|
||||
public void incrementCounter() {
|
||||
writeLock.lock();
|
||||
try {
|
||||
counter += 1;
|
||||
} finally {
|
||||
writeLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public int getCounter() {
|
||||
readLock.lock();
|
||||
try {
|
||||
return counter;
|
||||
} finally {
|
||||
readLock.unlock();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.concurrent.threadsafety.services;
|
||||
|
||||
public class StateHolder {
|
||||
|
||||
private final String state;
|
||||
|
||||
public StateHolder(String state) {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
public String getState() {
|
||||
return state;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.baeldung.concurrent.waitandnotify;
|
||||
|
||||
public class Data {
|
||||
private String packet;
|
||||
|
||||
// True if receiver should wait
|
||||
// False if sender should wait
|
||||
private boolean transfer = true;
|
||||
|
||||
public synchronized String receive() {
|
||||
while (transfer) {
|
||||
try {
|
||||
wait();
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
System.out.println("Thread Interrupted");
|
||||
}
|
||||
}
|
||||
transfer = true;
|
||||
|
||||
notifyAll();
|
||||
return packet;
|
||||
}
|
||||
|
||||
public synchronized void send(String packet) {
|
||||
while (!transfer) {
|
||||
try {
|
||||
wait();
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
System.out.println("Thread Interrupted");
|
||||
}
|
||||
}
|
||||
transfer = false;
|
||||
|
||||
this.packet = packet;
|
||||
notifyAll();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.concurrent.waitandnotify;
|
||||
|
||||
public class NetworkDriver {
|
||||
public static void main(String[] args) {
|
||||
Data data = new Data();
|
||||
Thread sender = new Thread(new Sender(data));
|
||||
Thread receiver = new Thread(new Receiver(data));
|
||||
|
||||
sender.start();
|
||||
receiver.start();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.concurrent.waitandnotify;
|
||||
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
public class Receiver implements Runnable {
|
||||
private Data load;
|
||||
|
||||
public Receiver(Data load) {
|
||||
this.load = load;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
for(String receivedMessage = load.receive();
|
||||
!"End".equals(receivedMessage) ;
|
||||
receivedMessage = load.receive()) {
|
||||
|
||||
System.out.println(receivedMessage);
|
||||
|
||||
//Thread.sleep() to mimic heavy server-side processing
|
||||
try {
|
||||
Thread.sleep(ThreadLocalRandom.current().nextInt(1000, 5000));
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
System.out.println("Thread Interrupted");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.concurrent.waitandnotify;
|
||||
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
public class Sender implements Runnable {
|
||||
private Data data;
|
||||
|
||||
public Sender(Data data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
String packets[] = {
|
||||
"First packet",
|
||||
"Second packet",
|
||||
"Third packet",
|
||||
"Fourth packet",
|
||||
"End"
|
||||
};
|
||||
|
||||
for (String packet : packets) {
|
||||
data.send(packet);
|
||||
|
||||
//Thread.sleep() to mimic heavy server-side processing
|
||||
try {
|
||||
Thread.sleep(ThreadLocalRandom.current().nextInt(1000, 5000));
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
System.out.println("Thread Interrupted");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<logger name="org.springframework" level="WARN" />
|
||||
<logger name="org.springframework.transaction" level="WARN" />
|
||||
|
||||
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
|
||||
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user