Even Odd number using Thread locking system - java

Today I was doing some practice on Thread and was trying to create one Even Odd number program. I created this using synchronized and it was working fine.
But when I tried to do the same thing using Lock then I stuck.
Below is the code that I am trying to do the same.
public class OddEvenNumberThreadLock {
public static void main(String args[]) {
SharedObject sharedObject = new SharedObject();
Thread evenThread = new Thread(new EvenNumber(sharedObject));
Thread oddThread = new Thread(new OddNumber(sharedObject));
evenThread.start();
oddThread.start();
}
}
class EvenNumber implements Runnable {
SharedObject object;
public EvenNumber(SharedObject object) {
this.object = object;
}
#Override
public void run() {
for (int i = 0; i <= 100; i = i + 2) {
while (!object.isOdd()) {
object.getLock().lock();
try {
System.out.println("Even : " + i);
object.setOdd(true);
} catch (Exception e) {
e.printStackTrace();
} finally {
object.getLock().unlock();
}
}
}
}
}
class OddNumber implements Runnable {
SharedObject object;
public OddNumber(SharedObject object) {
this.object = object;
}
#Override
public void run() {
for (int i = 1; i <= 100; i = i + 2) {
while (object.isOdd()) {
object.getLock().lock();
try {
System.out.println("Odd : " + i);
object.setOdd(false);
} catch (Exception e) {
e.printStackTrace();
} finally {
object.getLock().unlock();
}
}
}
}
}
class SharedObject {
private Lock lock;
private boolean isOdd;
public SharedObject() {
this.lock = new ReentrantLock();
}
public boolean isOdd() {
return isOdd;
}
public void setOdd(boolean isOdd) {
this.isOdd = isOdd;
}
public Lock getLock() {
return lock;
}
public void setLock(Lock lock) {
this.lock = lock;
}
}
I have one more question there like in the case of synchronized we use notify method to inform other thread. How we can achieve this thing in case of Lock.
Thanks

As far as I can tell you want to achieve that the two threads of yours print even and odd numbers in a ping-pong style. The behavior you want is easier to achieve with ReentrantLock than with synchronized block since synchronized is always unfair, but you can make ReentrantLock to be fair using the aproppriate constructor. Here is how your program would look like with Locks:
public class App {
public static void main(String args[]) {
SharedObject sharedObject = new SharedObject();
Thread evenThread = new Thread(new EvenNumber(sharedObject));
Thread oddThread = new Thread(new OddNumber(sharedObject));
evenThread.start();
oddThread.start();
}
}
class EvenNumber implements Runnable {
SharedObject object;
public EvenNumber(SharedObject object) {
this.object = object;
}
public void run() {
int i = 0;
while(i <= 100) {
object.getLock().lock();
try {
if (!object.isOdd()) {
System.out.println("Even : " + i);
i = i + 2;
object.setOdd(true);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
object.getLock().unlock();
}
}
}
}
class OddNumber implements Runnable {
SharedObject object;
public OddNumber(SharedObject object) {
this.object = object;
}
public void run() {
int i = 1;
while(i <= 100) {
object.getLock().lock();
try {
if(object.isOdd()) {
System.out.println("Odd : " + i);
i = i + 2;
object.setOdd(false);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
object.getLock().unlock();
}
}
}
}
class SharedObject {
private Lock lock;
private boolean isOdd;
public SharedObject() {
this.lock = new ReentrantLock(true);
}
public boolean isOdd() {
return isOdd;
}
public void setOdd(boolean isOdd) {
this.isOdd = isOdd;
}
public Lock getLock() {
return lock;
}
public void setLock(Lock lock) {
this.lock = lock;
}
}

Related

print even and odd using 2 threads

Hi I am trying to print even and odd using two threads namedly EvenThread and OddThread, some times I am getting correct result and some times not, could any one please help me.
package com.java8;
public class EvenOddExample {
public static synchronized void print(int i,String name){
System.out.println(i+"--->"+name);
}
public static void main(String[] args) throws InterruptedException {
EvenThread e= new EvenThread();
e.start();
OddThread o=new OddThread();
o.start();
}
public static class EvenThread extends Thread{
public void run() {
for(int i=0;i<10;i++){
if(i%2==0){
print(i,"Even");
}else{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
public static class OddThread extends Thread{
#Override
public void run() {
for(int i=1;i<10;i++){
if(i%2!=0){
print(i,"Odd");
}else{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
You need some signaling between the two threads. Putting synchronized on the print method simply guarantees, that only one thread can enter the method at a time. To put your threads into order Object.wait() and Object.notify{All}() methods can be used.
Actually this is some kind of the Sender-Receiver Synchronization Problem. Based on the example of the problem described here (Please read this page in order to understand how this synchronization works) I adapted your code. Additionally I used ExecutorService and Callable instead of extending Thread, which is bad-practice:
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class EvenOddExample {
private static boolean evensTurn = true;
private static Object monitor = new Object();
public static void print(int i, String name) {
System.out.println(i + "--->" + name);
}
public static void main(String[] args) throws InterruptedException {
final ExecutorService executorService = Executors.newFixedThreadPool(2);
executorService.submit(new EvenCallable());
executorService.submit(new OddCallable());
executorService.shutdown();
}
public static class EvenCallable implements Callable<Void> {
#Override
public Void call() throws InterruptedException {
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
synchronized (monitor) {
while (!evensTurn) { // not your turn?
monitor.wait(); // wait for monitor in a loop to handle spurious wakeups
}
print(i, "Even");
evensTurn = false; // next odd needs to run
monitor.notifyAll(); // wakeup the odd thread
}
} else {
Thread.sleep(1000);
}
}
return null;
}
}
public static class OddCallable implements Callable<Void> {
#Override
public Void call() throws InterruptedException {
for (int i = 1; i < 10; i++) {
if (i % 2 != 0) {
synchronized (monitor) {
while (evensTurn) {
monitor.wait();
}
print(i, "Odd");
evensTurn = true;
monitor.notifyAll();
}
} else {
Thread.sleep(1000);
}
}
return null;
}
}
}
synchronized is used to lock the access of another thread, when the locked object is free, it does not guarantee which is next called thread. You can use semaphore to make inter-thread communication:
private static Semaphore[] semaphores = {new Semaphore(0), new Semaphore(1)};
static void print(int i, String name) {
try {
semaphores[(i + 1) % 2].acquire();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
System.out.println(i + "--->" + name);
semaphores[i % 2].release();
}
public class EvenOddPrinter {
static boolean flag = true;
public static void main(String[] args) {
class Odd implements Runnable {
#Override
public void run() {
for (int i = 1; i <= 10;) {
if (EvenOddPrinter.flag) {
System.out.println(i + "--->odd");
i += 2;
EvenOddPrinter.flag = !EvenOddPrinter.flag;
}
}
}
}
class Even implements Runnable {
#Override
public void run() {
for (int i = 2; i <= 10;) {
if (!EvenOddPrinter.flag) {
System.out.println(i + "---->even");
i += 2;
EvenOddPrinter.flag = !EvenOddPrinter.flag;
}
}
}
}
Runnable odd = new Even();
Runnable even = new Odd();
Thread t1 = new Thread(odd, "Odd");
Thread t2 = new Thread(even, "Even");
t1.start();
t2.start();
}
}

Simulating Inter Thread Communication in java

I have created a singleton instance which i am getting in my two threads, one thread, i.e, AddEmployeeInfo is adding the employee details into ArrayList and notifies the other thread, i.e., DisplayEmployeeInfo which displays all the employee information which are added and then waits for further data,
please help me with what changes should i include to make it work and proper inter thread communication is being done :
class AddEmployeeInfo implements Runnable{
SingletonInstance inst;
public AddEmployeeInfo(SingletonInstance instance) {
inst = instance;
}
#Override
public void run() {
Employee e1 = new Employee();
e1.setEmpId(001);
e1.setEmpName("SK");
e1.setEmpExp(2);
inst.addEmployee(e1);
Employee e2 = new Employee();
e2.setEmpId(002);
e2.setEmpName("AMIT");
e2.setEmpExp(3);
inst.addEmployee(e2);
}
}
class DisplayEmployeeInfo implements Runnable{
SingletonInstance inst;
public DisplayEmployeeInfo(SingletonInstance instance) {
inst = instance;
}
#Override
public void run() {
while(true){
inst.displayEmployeeInfo();
}}}
public class EmployeeInfo {
public static void main(String[] args) {
SingletonInstance instance = new SingletonInstance();
Thread t1 = new Thread(new AddEmployeeInfo(instance));
Thread t2 = new Thread(new DisplayEmployeeInfo(instance));
t1.start();
t2.start();
}
}
class SingletonInstance{
public List<Employee> empDetails = new ArrayList<>();
boolean flag = false;
public synchronized void addEmployee(Employee emp){
if (flag) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
flag = true;
notify();
empDetails.add(emp);
}
public synchronized void displayEmployeeInfo() {
if (!flag) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for(Employee getDetails : empDetails){
System.out.println(getDetails.getEmpId() + " " + getDetails.getEmpName());
}
flag = false;
notify();
}
}

IllegalMonitorStateException on notify() when synchronized on an Integer

I'm new to using wait() and notify() in Java and I'm getting an IllegalMonitorStateException.
Main Code
public class ThreadTest {
private static Integer state = 0;
public static void main(String[] args) {
synchronized(state) {
System.out.println("Starting thread");
Thread t = new Thread(new AnotherTest());
t.start();
synchronized(state) {
state = 0;
while(state == 0) {
try {
state.wait(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("State is: " + state);
}
}
}
public static class AnotherTest implements Runnable {
#Override
public void run() {
synchronized(state) {
state = 1;
state.notify();
}
}
}
}
I'm getting an IllegalMonitorStateException what state.notify() is called. Any ideas?
Edit: Based on answer below here is code that works. As a side note, I was first trying this with an enum which has the same problem of using Integer.
public class ThreadTest {
private static int state = 0;
private static Object monitor = new Object();
public static void main(String[] args) {
synchronized(monitor) {
System.out.println("Starting thread");
Thread t = new Thread(new AnotherTest());
t.start();
state = 0;
while(state == 0) {
try {
for(int i = 0; i < 5; i++) {
System.out.println("Waiting " + (5 - i) + " Seconds");
Thread.sleep(1000);
}
monitor.wait(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("State is: " + state);
}
}
public static class AnotherTest implements Runnable {
#Override
public void run() {
synchronized(monitor) {
state = 1;
monitor.notify();
}
}
}
}
This
private static Integer state = 0;
is equivalent to
private static Integer state = Integer.valueOf(0);
The invocation of valueOf(0) returns a reference to an Integer object, call it A.
You then do
synchronized(state) {
your thread acquires the lock on the object referenced by state, currently that is A.
You then do
state = 1;
which is equivalent to
state = Integer.valueOf(1);
which gives you a different reference to an Integer object, call it B, and assigns it to state. When you then call
state.notify();
you're invoking notify() on an object, B, for which your thread doesn't own the monitor. You can't call notify or wait on objects for which your thread doesn't own the monitor.

Concurrency - why does the last instruction in this function never execute?

I have this code:
private void doSomething() throws InterruptedException {
WorkerThread w= new WorkerThread(this);
w.start();
synchronized (synchObj) {
while (!isDone) {
synchObj.wait();
}
}
System.out.println("End");
}
Where the calling class implements a method that calls notifyAll() on synchObj when WorkerThread instance is done. Everything works pretty much as expected except the final call to System.out.println("End"); is never called. Why is that?
Edit: Here's the rest of the code:
public class App implements Notifee {
private boolean isDone = false;
private final Object synchObj = new Object();
/**
* #param args
*/
public static void main(String[] args) {
App app = new App();
for (int i = 0; i < 5; i++) {
try {
app.doSomething();
} catch (InterruptedException e) {
System.err.println("Didn't even start");
e.printStackTrace();
}
}
}
private void doSomething() throws InterruptedException {
WorkerThread w= new WorkerThread(this);
w.start();
synchronized (synchObj) {
while (!isDone) {
synchObj.wait();
}
}
System.out.println("End");
}
#Override
public void letMeKnow() {
synchronized (synchObj) {
synchObj.notifyAll();
}
}
}
public class WorkerThread extends Thread {
private Notifee n;
public WorkerThread(Notifee n){
this.n = n;
}
#Override
public void run() {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
n.letMeKnow();
}
}
You are never setting isDone to true. Also you should make it volatile. You probably should add:
#Override
public void letMeKnow() {
isDone = true;
synchronized (synchObj) {
synchObj.notifyAll();
}
}
Edit: If you want to just wait for the worker thread to finish call:
w.join();

Java Thread - i want to generate numbers in sequence eg: 1,2,3,4...so on (there will be 2 threads only )

Java Thread - i want to generate numbers in sequence eg: 1,2,3,4... (there will be 2 threads only ) 1st thread o/p will be 1 ,second thread o/p will be 2 , again 1st thread o/p will be 3 and so on , it can be upto 10 or upto n number whatever just wanna get the logic please help me guys :|
below is my attempt to do it but its not working i know there would be wait() and notify() methods for sure but cant figure out the proper way to use them !
class NumberGenerator
{
static int number = 0;
synchronized public int numGenerator()
{
for(int i=0;i<20;i++)
{
System.out.println(i);
number=i;
}
return number;
}
}
class FirstThreadClass extends Thread
{
NumberGenerator num;
FirstThreadClass(NumberGenerator num)
{
this.num = num;
}
public void run()
{
System.out.println("i am from 1st thread :"+num.numGenerator());
}
}
class SecondThreadClass extends Thread
{
NumberGenerator num;
SecondThreadClass(NumberGenerator num)
{
this.num = num;
}
public void run()
{
System.out.println("i am from 2nd thread :"+num.numGenerator());
}
}
public class ThreadTesting {
public static void main(String[] args) {
FirstThreadClass ftc = new FirstThreadClass(new NumberGenerator());
SecondThreadClass stc = new SecondThreadClass(new NumberGenerator());
ftc.start();
stc.start();
}
}
class NumberGenerator
{
static int counter = 0;
public synchronized int getNextNumber()
{
return counter++;
}
}
class FirstThreadClass
extends Thread
{
NumberGenerator num;
FirstThreadClass(NumberGenerator num)
{
this.num = num;
}
public void run()
{
System.out.println("i am from 1st thread :" + num.getNextNumber());
}
}
class SecondThreadClass
extends Thread
{
NumberGenerator num;
SecondThreadClass(NumberGenerator num)
{
this.num = num;
}
public void run()
{
System.out.println("i am from 2nd thread :" + num.getNextNumber());
}
}
public class ThreadTesting
{
public static void main(String[] args)
{
FirstThreadClass ftc = new FirstThreadClass(new NumberGenerator());
SecondThreadClass stc = new SecondThreadClass(new NumberGenerator());
for (int k = 0; k < 10; k++)
{
ftc.run();
stc.run();
}
}
}
You can have each thread generate numbers as follows:
Thread 1: 1, 3, 5, 7, 9, ...
Thread 2: 2, 4, 6, 8, 10, ...
add to a concurrent collection and sort afterwards.
Do they have to generate only one each time, or is it ok if thread1 generate 2 numbers, then thread 2 generates 1 number etc... ?
Use a static int field that will act as a counter, and access it in a synchronized way.
static int counter = 0;
public synchronized int getNextNumber(){
return counter++;
}
Then the threads do :
while(...whatever..){
System.out.print(getNextNumber());
}
you can achieve this using cyclic barrier, create a barrier and once two threads have generated one number each print the two numbers
class ThreadTest {
private CyclicBarrier cyclicBarrier = new CyclicBarrier(2, new Runnable() {
#Override
public void run() {
System.out.println(oddNumberGenerator.result);
System.out.println(evenNumberGenerator.result);
}
});
private NumberGenerator oddNumberGenerator = new NumberGenerator(1,11,2);
private NumberGenerator evenNumberGenerator = new NumberGenerator(2,10,2);
public void generateSeries(){
oddNumberGenerator.generateNumbers();
evenNumberGenerator.generateNumbers();
}
class NumberGenerator {
private Thread thread;
private int result;
private NumberGenerator(final int initialValue, final int maxValue,
final int stepSize) {
this.thread = new Thread(new Runnable() {
#Override
public void run() {
for (int i = initialValue; i <= maxValue; i = i + stepSize) {
try {
result = i;
cyclicBarrier.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
}
}
});
}
public void generateNumbers() {
thread.start();
}
}
main(String[] args){
new ThreadTest().generateSeries();
}
}
You can achieve using wait and notifyAll() . But it is always better to use standard java concurrent classes to achieve it
public class PrintAlternateValues {
public static void main(String[] args) {
final NumberValue number = new NumberValue();
final Object lockObject = new Object();
new Thread(){
private NumberValue n = number;
#Override
public void run() {
synchronized (lockObject) {
while(n.getValue() < n.getEndPoint()){
while(n.isToggle()){
try{
lockObject.wait();
}catch(Exception e){
e.printStackTrace();
}
}
n.incrementValue();
System.out.println(getName() + " printing "+n.getValue());
n.setToggle(true);
lockObject.notifyAll();
}
}
}
}.start();
new Thread(){
private NumberValue n = number;
#Override
public void run() {
synchronized (lockObject) {
while(n.getValue() < n.getEndPoint()){
while(!n.isToggle()){
try{
lockObject.wait();
}catch(Exception e){
e.printStackTrace();
}
}
n.incrementValue();
System.out.println(getName() + " printing "+n.getValue());
n.setToggle(false);
lockObject.notifyAll();
}
}
}
}.start();
}
}
class NumberValue {
private int value;
private boolean toggle = true;
private int endPoint = 10;
public int getEndPoint() {
return endPoint;
}
public void setEndPoint(int endPoint) {
this.endPoint = endPoint;
}
public boolean isToggle() {
return toggle;
}
public void setToggle(boolean toggle) {
this.toggle = toggle;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public synchronized void incrementValue(){
this.value++;
}
}

Categories