So many interviewer ask this question, Where one thread prints even and another thread prints odd number.
This problem statement is specification of producer and consumer problem in threading. Only difference is Even and odd thread should be executed but same time none of thread must not run more than 1 time continuously .
How to solve this problem using OOPS concepts :
Below class will be behaved as shared resource between 2 threads:
class B {
int i = 0;
boolean status = true;
public synchronized void evenPrint() throws InterruptedException {
if (!status) {
wait();
}
notifyAll();
status = !status;
System.out.println("Even Thread--"+i);
i = i+1;
}
public synchronized void oddPrint() throws InterruptedException {
if (status) {
wait();
}
notifyAll();
System.out.println("Odd Thread--"+i);
i = i+1;
status = !status;
}
}
Above class has a int variable and its value be incremented by 2 asap completes its execution, there is another variable to control the flow of execution of threads. There are 2 method which are synchronized and will be executed by respective threads.
1) Even Thread:
class EvenThread implements Runnable {
B a;
@Override
public void run() {
int i = 0;
while (i < 10) {
try {
a.evenPrint();
i++;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public EvenThread(B a) {
this.a = a;
}
}
2) Odd Thread:
class OddThread implements Runnable {
B a;
int i = 0;
@Override
public void run() {
while (i < 10) {
try {
a.oddPrint();
i++;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public OddThread(B a) {
this.a = a;
}
}
Main Class:
public class EvenOddPrinting {
public static void main(String[] args) {
B a = new B();
Thread even = new Thread(new EvenThread(a));
Thread odd = new Thread(new OddThread(a));
even.start();
odd.start();
}
}
when you run this program, output will be :
Even Thread--0
Odd Thread--1
Even Thread--2
Odd Thread--3
Even Thread--4
Odd Thread--5
Even Thread--6
Odd Thread--7
Even Thread--8
Odd Thread--9
Even Thread--10
Odd Thread--11
Even Thread--12
Odd Thread--13
Even Thread--14
Odd Thread--15
Even Thread--16
Odd Thread--17
Even Thread--18
Odd Thread--19
This problem statement is specification of producer and consumer problem in threading. Only difference is Even and odd thread should be executed but same time none of thread must not run more than 1 time continuously .
How to solve this problem using OOPS concepts :
Below class will be behaved as shared resource between 2 threads:
class B {
int i = 0;
boolean status = true;
public synchronized void evenPrint() throws InterruptedException {
if (!status) {
wait();
}
notifyAll();
status = !status;
System.out.println("Even Thread--"+i);
i = i+1;
}
public synchronized void oddPrint() throws InterruptedException {
if (status) {
wait();
}
notifyAll();
System.out.println("Odd Thread--"+i);
i = i+1;
status = !status;
}
}
Above class has a int variable and its value be incremented by 2 asap completes its execution, there is another variable to control the flow of execution of threads. There are 2 method which are synchronized and will be executed by respective threads.
1) Even Thread:
class EvenThread implements Runnable {
B a;
@Override
public void run() {
int i = 0;
while (i < 10) {
try {
a.evenPrint();
i++;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public EvenThread(B a) {
this.a = a;
}
}
2) Odd Thread:
class OddThread implements Runnable {
B a;
int i = 0;
@Override
public void run() {
while (i < 10) {
try {
a.oddPrint();
i++;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public OddThread(B a) {
this.a = a;
}
}
Main Class:
public class EvenOddPrinting {
public static void main(String[] args) {
B a = new B();
Thread even = new Thread(new EvenThread(a));
Thread odd = new Thread(new OddThread(a));
even.start();
odd.start();
}
}
when you run this program, output will be :
Even Thread--0
Odd Thread--1
Even Thread--2
Odd Thread--3
Even Thread--4
Odd Thread--5
Even Thread--6
Odd Thread--7
Even Thread--8
Odd Thread--9
Even Thread--10
Odd Thread--11
Even Thread--12
Odd Thread--13
Even Thread--14
Odd Thread--15
Even Thread--16
Odd Thread--17
Even Thread--18
Odd Thread--19
No comments:
Post a Comment