Skip to content

Instantly share code, notes, and snippets.

@anandrajneesh
Created August 15, 2018 07:18
Show Gist options
  • Save anandrajneesh/b5ab3bfe57d791f5c6ea15f45e863f91 to your computer and use it in GitHub Desktop.
Save anandrajneesh/b5ab3bfe57d791f5c6ea15f45e863f91 to your computer and use it in GitHub Desktop.
package multithreading;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
public class PingPongOneRunnable {
private final static class Play implements Runnable{
private final String shot;
private final Consumer<AtomicInteger> counterUpdater;
private final Function<AtomicInteger, Boolean> whilePredicate;
private final Supplier<Boolean> shouldStart;
private final Runnable started;
private final Object lock;
private final AtomicInteger counter;
private Play(String shot, Consumer<AtomicInteger> counterUpdater, Function<AtomicInteger, Boolean> whilePredicate, Supplier<Boolean> shouldStart, Runnable started, Object lock, AtomicInteger counter) {
this.shot = shot;
this.counterUpdater = counterUpdater;
this.whilePredicate = whilePredicate;
this.shouldStart = shouldStart;
this.started = started;
this.lock = lock;
this.counter = counter;
}
@Override
public void run() {
if(shouldStart.get()){
synchronized (lock){
started.run();
while (whilePredicate.apply(counter)){
System.out.println(shot + " "+ counter.get());
counterUpdater.accept(counter);
lock.notify();
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
lock.notify();
}
}
}
}
public static void main(String[] args) {
try{
AtomicInteger counter = new AtomicInteger(0);
CountDownLatch startLatch = new CountDownLatch(1);
Object lock = new Object();
Play ping = new Play("Ping", (arg) -> {}, (arg) -> arg.get()< 10, () -> true, startLatch::countDown, lock, counter);
Play pong = new Play("Pong", AtomicInteger::incrementAndGet, (arg) -> arg.get()< 10, () -> {
try {
startLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
return true;
}, () -> {}, lock, counter);
Thread t2 = new Thread(ping);
Thread t1 = new Thread(pong);
t1.start();
t2.start();
t1.join();
t2.join();
}catch (Exception e){
e.printStackTrace();
}
}
}
package multithreading;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
public class PingPongTwoRunnables {
private static final class Play implements Runnable {
private final String shot;
private Play(String shot) {
this.shot = shot;
}
@Override
public void run() {
System.out.println(shot);
}
}
private static final class Ping implements Runnable {
private final AtomicInteger counter;
private final Object lock;
private final CountDownLatch startLatch;
private Ping(AtomicInteger counter, Object lock, CountDownLatch startLatch) {
this.counter = counter;
this.lock = lock;
this.startLatch = startLatch;
}
@Override
public void run() {
try {
Thread.sleep(10000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (lock) {
while (counter.get() < 10) {
System.out.println("ping" + counter.get());
if (counter.get() == 0) {
startLatch.countDown();
}
lock.notify();
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
lock.notify();
}
}
}
private static final class Pong implements Runnable {
private final AtomicInteger counter;
private final Object lock;
private final CountDownLatch startLatch;
private Pong(AtomicInteger counter, Object lock, CountDownLatch startLatch) {
this.counter = counter;
this.lock = lock;
this.startLatch = startLatch;
}
@Override
public void run() {
try {
startLatch.await();
while (counter.get() < 10) {
synchronized (lock) {
System.out.println("pong " + counter.get());
counter.incrementAndGet();
lock.notify();
lock.wait();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
try {
Object lock = new Object();
CountDownLatch startLatch = new CountDownLatch(1);
AtomicInteger counter = new AtomicInteger(0);
Thread t1 = new Thread(new Ping(counter, lock, startLatch));
Thread t2 = new Thread(new Pong(counter, lock, startLatch));
t1.start();
t2.start();
//t1.join();
//t2.join();
System.out.println("leaving main");
} catch (Exception e) {
e.printStackTrace();
}
}
}
package multithreading;
import java.util.UUID;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class ProducerConsumerBlockingQueue {
private final static class Producer implements Runnable{
private final BlockingQueue<String> queue;
private Producer(BlockingQueue<String> queue) {
this.queue = queue;
}
@Override
public void run() {
for(int i = 0 ; i < 10; i ++){
String msg = UUID.randomUUID().toString();
System.out.println("Adding msg "+msg + " at counter "+ i);
try {
queue.put(msg);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
private final static class Consumer implements Runnable{
private final BlockingQueue<String> queue;
private Consumer(BlockingQueue<String> queue) {
this.queue = queue;
}
@Override
public void run() {
int count = 0;
while(count < 10) {
try {
String msg = queue.take();
System.out.println("Received msg " + msg + " at counter " + count);
count++;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) throws InterruptedException {
BlockingQueue<String> queue = new ArrayBlockingQueue<>(3);
Producer producer = new Producer(queue);
Consumer consumer = new Consumer(queue);
Thread t1 = new Thread(producer);
Thread t2 = new Thread(consumer);
t1.start();
t2.start();
t1.join();
t2.join();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment