Create two threads, one display odd & other even numbers
I would just change a few details (no need to use the modulo operator here...):
public class Mythread {
public static void main(String[] args) {
Runnable r = new Runnable1();
Thread t = new Thread(r);
Runnable r2 = new Runnable2();
Thread t2 = new Thread(r2);
t.start();
t2.start();
}
}
class Runnable2 implements Runnable{
public void run(){
for(int i=0;i<11;i+=2) {
System.out.println(i);
}
}
}
class Runnable1 implements Runnable{
public void run(){
for(int i=1;i<=11;i+=2) {
System.out.println(i);
}
}
}
Yes it is fine. But in this case, I don't think you need 2 threads are all, because the operation is simple. However, if you are practicing threads, it is OK
@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.