Printing even and odd number by two thread - multithreading

I am trying to print even and odd number using two different thread but It is throwing IllegalMonitorStateException .Kindly suggest me what am I doing wrong .
package com.rishi.threading;
public class MyThread {
public static class EvenThread extends Thread {
Boolean evenFlag;
Integer sharedCount;
public EvenThread(Boolean flag, Integer count) {
this.evenFlag = flag;
this.sharedCount = count;
}
public void run() {
printEven();
}
private void printEven() {
for (int i = 0; i < 10; i++) {
synchronized (evenFlag) {
if (!evenFlag) {
try {
evenFlag.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
evenFlag = false;
System.out.println("even thread :" + sharedCount++);
notifyAll();
}
}
}
}
public static class OddThread extends Thread {
Boolean evenflag;
Integer sharedCount;
public OddThread(Boolean flag, Integer count) {
this.evenflag = flag;
this.sharedCount = count;
}
public void run() {
printOdd();
}
private void printOdd() {
// TODO Auto-generated method stub
for (int i = 0; i < 10; i++) {
synchronized (evenflag) {
if (evenflag) {
try {
evenflag.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
evenflag = true;
System.out.println("odd thread :" + sharedCount++);
notifyAll();
}
}
}
}
public static void main(String[] args) {
Boolean evenFlag = new Boolean(true);
Integer sharedCount = new Integer(0);
EvenThread thread1 = new EvenThread(evenFlag, sharedCount);
thread1.setName("evenThread");
OddThread thread2 = new OddThread(evenFlag, sharedCount);
thread2.setName("oddthread");
thread1.start();
thread2.start();
}
}
I created two thread "EvenThread" and "OddThread".
Both the thread shares two common member variable ie evenFlag and sharedCount.
evenFlag -> just a flag which is set as true when sharedCount is even and set as false when shareDcount is set to odd number.
So both the thread is trying to get lock of evenFlag to control the printing of sharedCount.

Related

Why this code is giving IllegalMonitor Exception, when doing synchronization on instance variable

If I do synchronization on the lock, an Object, it works properly. However, if I do synchronization on number, an Integer, it throws an exception. Please help.
Below is my code:
package synchronization;
public class EvenOddThread implements Runnable {
int reminder;
Integer number = 1;
static Object lock = new Object();
public EvenOddThread( int reminder)
{
this.reminder = reminder;
}
public void run()
{
while(number < 10)
{
synchronized (number)
{
while(number %2 != reminder)
{
try
{
System.out.println(Thread.currentThread().getName()+" is waiting at "+number+" "+reminder);
number.wait();
}
catch(Exception e)
{
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+ " "+number );
number = number +1;
System.out.println(number);
//synchronized (lock) {
number.notifyAll();
//}
}
}
}
}

[Print alternatively using alternate Threads]

I am trying to implement this:
First Thread prints 1,Second Thread prints 2,Third Thread prints 3,First Thread prints 4 and so on:
I did this and its working :
class Display implements Runnable
{
int threadId;
static int v=1;
static int turn=1;//init
static Object monitor=new Object();
Display(int id)
{
this.threadId=id;
}
public void run() {// TODO Auto-generated method stub
print();
}
private void print() {
// TODO Auto-generated method stub
while(v<100)
{
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
synchronized(monitor)
{
if(threadId==1)
{
if(turn!=1)
{
try {
monitor.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
{
System.out.println(threadId + ":"+v);
v++;
turn=2;
monitor.notifyAll();
}
}
else if(threadId==2)
{
if(turn!=2)
{
try {
monitor.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
{
System.out.println(threadId+":"+v);
v++;
turn=3;
monitor.notifyAll();
}
}
else
{
if(turn!=3)
{
try {
monitor.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
{
System.out.println(threadId+":"+v);
v++;
turn=1;
monitor.notifyAll();
}
}
}
}
}
}
public class Print {
public static void main(String[] ar)
{
Thread t1= new Thread(new Display(1));
Thread t2= new Thread(new Display(2));
Thread t3= new Thread(new Display(3));
t1.start();
t2.start();
t3.start();
}
}
Output is like this:
1:1
2:2
3:3
1:4
2:5
It achieves the purpose but if I am having two more Threads to print alternatively,then I have to use more If conditions.
Can Anyone suggest to write this code in a better form to achieve that task in a more clean way so that it is extendable if more Threads are added.
You will need the highest Id given to your threads. After this, you can simplify your code using the modulo operator.
class Display implements Runnable {
int threadId;
static int v = 1;
static int higehstId = 0;
static Object monitor = new Object();
Display(int id) {
this.threadId = id;
}
public void run() {
print();
}
private void print() {
while (v < 100) {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
synchronized (monitor) {
if ((v - 1) % higehstId == threadId - 1) {
System.out.println(threadId + ":" + v);
v++;
monitor.notifyAll();
} else {
try {
monitor.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public static void main(String[] ar) {
Thread t1 = new Thread(new Display(1));
Thread t2 = new Thread(new Display(2));
Thread t3 = new Thread(new Display(3));
Thread t4 = new Thread(new Display(4));
Thread t5 = new Thread(new Display(5));
Display.higehstId = 5;
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
}
}
If you initialize v to 0 and start numbering the Threads from 0, you could simplify the if statement to this:
if(v % highestId == threadId)

Implementing custom Executor

In the example below if I implement ExecutorImpl without using Thread, then taskCompletionService.submit is blocked, even though it returns Future.
Is it possible to not block submit, but not use Thread in ExecutorImpl?
class ExecutorServiceTest {
private static class ExecutorImpl implements Executor {
public void execute(Runnable r) {
final Thread t = new Thread(new Runnable() {
public void run() {
r.run();
}});
t.start();
//If used will block others.
//r.run();
}
}
public static void main(String[] args) throws InterruptedException, ExecutionException {
final Executor executor = new ExecutorImpl();
final CompletionService<String> taskCompletionService = new ExecutorCompletionService<>(executor);
int submittedTasks = 3;
for(int i = 0; i < submittedTasks; i++) {
final int j = i;
//here it is blocked if ExecutorServiceIml doesn't utilize Thread
taskCompletionService.submit(new Callable<String>() {
public String call() throws Exception {
Thread.sleep((3 - j) * 1000);
return "callable:" + String.valueOf(j);
}
});
System.out.println("Task " + String.valueOf(i) + " has been submitted...");
}
for(int tasksHandled=0; tasksHandled < submittedTasks; tasksHandled++) {
try {
final Future<String> result = taskCompletionService.take();
String l = result.get();
System.out.println("Task has completed - result: " + l);
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
}
}

PingPong Program Java Multithreading

I am trying to learn basic concept of Multi Threading.
Why my ping pong program prints only Ping0 & Pong0, Why notify() does not start the Ping Thread which is in Wait state?
public class PingPong implements Runnable {
String word;
public PingPong(String word) {
this.word = word;
}
public void run() {
synchronized (this) {
for (int i = 0; i < 10; i++) {
System.out.println(word + i);
try {
wait();
notifyAll();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
}
public static void main(String[] args) {
Runnable p1 = new PingPong("ping");
Thread t1 = new Thread(p1);
t1.start();
Runnable p2 = new PingPong("pong");
Thread t2 = new Thread(p2);
t2.start();
}
}
Output
ping0
pong0
I tried removing wait() and it's printing ping pong till the end of loop. but is this guaranteed that it will print in sequence?
Why wait() followed by notify() does not ask the ping1 thread to start execution?
If you see the jstack, you can see thread-0 and thread-1 are waiting for different lock. That's because your p1 and p2 are different objects, so when you use synchronized (this), they are not competing for the same lock, so notify this way won't work. try use another object as the lock.
wait need to run after notify. As when both threads go into waiting stat, no other threads can notify them.
try this code:
String word;
Object a;
public PingPong(String word, Object a) {
this.word = word;
this.a = a;
}
public void run() {
synchronized (a) {
for (int i = 0; i < 10; i++) {
System.out.println(word + i);
try {
a.notifyAll();
a.wait();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
}
public static void main(String[] args) throws InterruptedException {
Object a = new Object();
Runnable p1 = new PingPong("ping", a);
Thread t1 = new Thread(p1);
t1.start();
Runnable p2 = new PingPong("pong", a);
Thread t2 = new Thread(p2);
t2.start();
}
Here is a similar solution using Thread Pool Executors:
public class PingPong implements Runnable {
String word;
Lock lock;
public PingPong(String word, Lock lock) {
this.word = word;
this.lock = lock;
}
#Override
public void run() {
while(true){
System.out.println("Received : " + word);
lock.notifyAll();
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
ExecutorService ex = Executors.newFixedThreadPool(2);
Lock lock = new ReentrantLock();
while(true){
ex.submit(new PingPong("ping", lock));
ex.submit(new PingPong("pong", lock));
}
}
}
Below solution based on:
Java internal API
order of execution
public class Test {
public static void main(String[] args) {
SynchronousQueue<String> queue = new SynchronousQueue<>();
Thread ping = new Thread(new Task(queue, "ping", "ping"));
ping.setName("ping thread");
ping.start();
Thread pong = new Thread(new Task(queue, "pong", "ping"));
pong.setName("pong thread");
pong.start();
}
private static class Task implements Runnable {
private SynchronousQueue<String> queue;
private String command;
private String step;
Task(SynchronousQueue<String> queue, String command, String step) {
this.queue = queue;
this.command = command;
this.step = step;
}
#Override
public void run() {
try {
if (command.equals(step)) {
doCommandAndWaitRepeatedly();
} else {
waitAndDoCommandRepeatedly();
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
private void doCommandAndWaitRepeatedly() throws InterruptedException {
while (true) {
queue.offer(command, 1, TimeUnit.SECONDS);
Thread.sleep(500);
System.out.println(Thread.currentThread().getName() + ":" + queue.poll(1, TimeUnit.SECONDS));
}
}
private void waitAndDoCommandRepeatedly() throws InterruptedException {
while (true) {
System.out.println(Thread.currentThread().getName() + ":" + queue.poll(1, TimeUnit.SECONDS));
Thread.sleep(500);
queue.offer(command, 1, TimeUnit.SECONDS);
}
}
}
}
class Ping extends Thread
{
public void run()
{
for(int i=1;i<=5;i++)
{
System.out.println("PING");
try{
sleep(2000);
} catch(Exception e){}
}
}
}
class Pong extends Thread
{
public void run()
{
for (int i=1;i<=5;i++)
{
System.out.println("PONG");
try{
sleep(2000);
} catch(Exception e){}
}
}
}
public class PingPong
{
public static void main(String... args) throws Exception
{
Ping p1=new Ping();
Pong p2=new Pong();
p1.start();
Thread.sleep(1000); //super important for proper sequence
p2.start();
p1.join();
}
}

Thread synchronization with multiple objects

I have been facing this problem for many days, Please help me out. I am implementing producer-consumer example using thread synchronization. I have made some twist in this traditional program. Instead of using only one queue object, i have used two Queue objects. But program is not working.. (PS- I know i can make this program work by using only one object of queue, But what if i want to use two queue objects ?? )
class Queue {
static int value;
static boolean valueSet = false;
public static final Object obj;
static {
obj = new Object();
}
void push() {
synchronized(Queue.obj) {
while( Queue.valueSet ) {
try {
Thread.sleep(1000);
}catch(Exception e) {}
}
System.out.print("\n\n Push:- " + (++(Queue.value)));
Queue.valueSet = true;
return;
}
}
void pop() {
synchronized(Queue.obj) {
while(!(Queue.valueSet)) {
try {
Thread.sleep(1000);
}catch(Exception e) {}
}
System.out.print("\n\n Pop:- " + Queue.value);
Queue.valueSet = false;
return;
}
}
}
class Producer implements Runnable {
Queue Q;
Thread P;
Producer(Queue Q) {
this.Q = Q;
P = new Thread(this);
P.start();
}
public void run() {
while(true) {
Q.push();
}
}
}
class Consumer implements Runnable {
Queue Q;
Thread C;
Consumer(Queue Q) {
this.Q = Q;
C = new Thread(this);
C.start();
}
public void run() {
while(true) {
Q.pop();
}
}
}
public class TestQueue {
public static void main(String[] args) {
Queue Q1 = new Queue();
Queue Q2 = new Queue();
Object obj = new Object();
Producer p = new Producer(Q1);
Consumer c = new Consumer(Q2);
}
}
I got the answer. My misconception was wait,notify and notifyall methods are of thread class. So i was invoking them on thread object.
Solution is to just invoke wait and notify method on shared static object other than thread.
Answer:-
class Queue {
static int value;
static boolean valueSet = false;
public static final Object obj;
static {
obj = new Object();
}
void push() {
synchronized(Queue.obj) {
while( Queue.valueSet ) {
try {
Queue.obj.wait();
Thread.sleep(1000);
}catch(Exception e) {
e.printStackTrace();
}
}
System.out.print("\n\n Push:- " + (++(Queue.value)));
Queue.valueSet = true;
Queue.obj.notify();
}
}
void pop() {
synchronized(Queue.obj) {
while(!(Queue.valueSet)) {
try {
Queue.obj.wait();
Thread.sleep(1000);
}catch(Exception e) {
e.printStackTrace();
}
}
System.out.print("\n\n Pop:- " + Queue.value);
Queue.valueSet = false;
Queue.obj.notify();
}
}
}
class Producer implements Runnable {
Queue Q;
Thread P;
Producer(Queue Q) {
this.Q = Q;
P = new Thread(this);
P.start();
}
public void run() {
while(true) {
Q.push();
}
}
}
class Consumer implements Runnable {
Queue Q;
Thread C;
Consumer(Queue Q) {
this.Q = Q;
C = new Thread(this);
C.start();
}
public void run() {
while(true) {
Q.pop();
}
}
}
public class TestQueue {
public static void main(String[] args) {
Queue Q1 = new Queue();
Queue Q2 = new Queue();
Producer p = new Producer(Q1);
Consumer c = new Consumer(Q2);
}
}

Resources