Monday, 29 November 2021

Print Zero,Even,Odd numbers in this order(0,1,0,2,0,3,0,4...).Threads started at the same time



import java.util.concurrent.Semaphore;

import java.util.function.IntConsumer;


public class ZeroEvenOdd {


private int n, lastPrinted;

private Semaphore oddSem;

private Semaphore zeroSem;

private Semaphore evenSem;


public ZeroEvenOdd(int n) {

this.n = n;

this.lastPrinted = 0;

this.zeroSem = new Semaphore(1);

this.oddSem = new Semaphore(1);

this.evenSem = new Semaphore(1);


try {

this.evenSem.acquire();

this.oddSem.acquire();

} catch (InterruptedException e) {

throw new RuntimeException(e);

}

}


public void zero(IntConsumer printNumber) throws InterruptedException {


int numZeros = n;

while (numZeros-- > 0) {

this.zeroSem.acquire();

printNumber.accept(0);

if (lastPrinted % 2 == 0) {

this.oddSem.release();

} else {

this.evenSem.release();

}

}

}


public void even(IntConsumer printNumber) throws InterruptedException {


int numeven = n / 2;

while (numeven-- > 0) {

this.evenSem.acquire();

printNumber.accept(++lastPrinted);

this.zeroSem.release();

}


}


public void odd(IntConsumer printNumber) throws InterruptedException {

int numOdd = n - n / 2;

while (numOdd-- > 0) {

this.oddSem.acquire();

printNumber.accept(++lastPrinted);

this.zeroSem.release();

}


}


public static void main(String[] args) {

ZeroEvenOdd print = new ZeroEvenOdd(5);

Thread thread = new Thread() {

public void run() {

System.out.println("Thread Running");

IntConsumer ic = (x) -> System.out.println(x);

try {

print.zero(ic);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

};

Thread thread2 = new Thread() {

public void run() {

System.out.println("Thread Running");

IntConsumer ic = (x) -> System.out.println(x +"\t");

try {

print.odd(ic);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

};

Thread thread3 = new Thread() {

public void run() {

System.out.println("Thread Running");

IntConsumer ic = (x) -> System.out.println(x);

try {

print.even(ic);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

};

thread.start();

thread2.start();

thread3.start();


}

}


Three threads started at the same time .Priniting Order should be maintained , it should always print first,second,third

 package printorder;


import java.util.concurrent.Semaphore;

import java.util.function.IntConsumer;


public class PrintOrder {


private Semaphore first;

private Semaphore second;

private Semaphore third;


public PrintOrder() {

this.first = new Semaphore(1);

this.second = new Semaphore(1);

this.third = new Semaphore(1);


try {

this.second.acquire();

this.third.acquire();

} catch (InterruptedException e) {

e.printStackTrace();

}


}


public void first() throws InterruptedException {

this.first.acquire();

print("first");


this.second.release();

}


private void print(String string) {

System.out.println("Print order :" + string);


}


public void second() throws InterruptedException {

this.second.acquire();

print("second");


this.third.release();

}


public void third() throws InterruptedException {

this.third.acquire();

print("third");


this.first.release();

}


public static void main(String[] args) {


PrintOrder order = new PrintOrder();

Thread t1 = new Thread() {

public void run() {

System.out.println("Thread Running t1 "+Thread.currentThread().getName());

try {


order.third();

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

};

Thread t2 = new Thread() {

public void run() {

System.out.println("Thread Running t2 "+Thread.currentThread().getName());

try {


order.second();

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

};

Thread t3 = new Thread() {

public void run() {

System.out.println("Thread Running t3 "+Thread.currentThread().getName());

try {


order.first();

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

};


t1.start();

t2.start();

t3.start();


}


}


Pass a HashMap from Angular Client to Spring boot API

This example is for the case where fileData is very huge and in json format   let map = new Map<string, string>()      map.set(this.ge...