I'm trying to create two threads, one thread display even integers from 0 to 10, one thread display odd integers from 1 to 11. Is the following code suitable for designing this program?
public class Mythread { public static void main(String[] args) { Runnable r = new Runnable1(); Thread t = new Thread(r); t.start(); Runnable r2 = new Runnable2(); Thread t2 = new Thread(r2); t2.start(); } } class Runnable2 implements Runnable{ public void run(){ for(int i=0;i<11;i++){ if(i%2 == 1) System.out.println(i); } } } class Runnable1 implements Runnable{ public void run(){ for(int i=0;i<11;i++){ if(i%2 == 0) System.out.println(i); } } }
We create two threads, i.e., thread1 and thread2, for even and odd numbers simultaneously. The thread1 will call printEvenNumbers() method and the thread2 will call printOddNumbers() method simultaneously. The thread1 will wait till the value of boolean odd is true in printEvenNumbers().
Create 2 Semaphore s and pass them to 2 threads: Semaphore a = new Semaphore(1); // first thread is allowed to run immediately Semaphore b = new Semaphore(0); // second thread has to wait ThreadPrinter tp1 = new ThreadPrinter(1, a, b); ThreadPrinter tp2 = new ThreadPrinter(2, b, a);
printOddNumber() is used to print all the odd numbers till N, printEvenNumber() is used to print all the even numbers till N.
@aymeric answer wont print the numbers in their natural order, but this code will. Explanation at the end.
public class Driver { static Object lock = new Object(); public static void main(String[] args) { Thread t1 = new Thread(new Runnable() { public void run() { for (int itr = 1; itr < 51; itr = itr + 2) { synchronized (lock) { System.out.print(" " + itr); try { lock.notify(); lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } }); Thread t2 = new Thread(new Runnable() { public void run() { for (int itr = 2; itr < 51; itr = itr + 2) { synchronized (lock) { System.out.print(" " + itr); try { lock.notify(); if(itr==50) break; lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } }); try { t1.start(); t2.start(); t1.join(); t2.join(); System.out.println("\nPrinting over"); } catch (Exception e) { } } }
In order to achieve so, the run methods of the two threads above have to be called one after the other, i.e. they need to be synchronized and I am achieving that using locks.
The code works like this: t1.run prints the odd number and notifies any waiting thread that it is going to release the lock, then goes into a wait state.
At this point t2.run is invoked, it prints the next even number, notifies other threads that it is about to release the lock it holds and then goes into wait state.
This continues till the itr in t2.run() reaches 50, at this point our goal has been achieved and we need to kill these two threads.
By breaking, I avoid calling lock.wait() in t2.run and t2 thread is thus shutdown, the control will now go to t1.run since it was waiting to acquire the lock; but here itr value will be > 51 and we will come out of its run(), thus shutting down the thread.
If break is not used in t2.run(), though we will see numbers 1 to 50 on the screen but the two threads will get into a deadlock situation and continue to be in wait state.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With