We have seen a lot of magic and usu abilities of Java Threads, currently i have encountered a problem which is as follows.
Write down a java program here two threads can print numbers simultaneously.
We have solved this problem from following java program.
Courtesy: Stack-overflow
Solution Explained: Before go into the explanation of each of solution i would like to remind some of the jargon here.
1. Join: So in multi threading environment multi threads always scheduled by a common scheduler which can not guarantee the execution of thread in terms of start and end of thread. but when we use join, it will provide the privilege of joining multi threads threads simultaneously.
Write down a java program here two threads can print numbers simultaneously.
We have solved this problem from following java program.
Courtesy: Stack-overflow
package roundOne;
public class Q1 {
public static void main(String[] args) {
Odd oddThread = new Odd();
Even evenThread = new Even();
Thread odd = new Thread(oddThread);
Thread even = new Thread(evenThread);
odd.start();
even.start();
try {
odd.join();
even.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class Odd implements Runnable {
@Override
public void run() {
synchronized (util.mlock) {
System.out.println();
int i = 1;
while (i < 50) {
System.out.println(i + "\t");
i = i + 2;
util.mlock.notify();
try {
util.mlock.wait();
} catch (Exception e) {
// TODO: handle exception
}
}
util.mlock.notify();
}
}
}
class Even implements Runnable {
@Override
public void run() {
synchronized (util.mlock) {
System.out.println();
int i = 2;
while (i < 50) {
System.out.println(i + "\t");
i = i + 2;
util.mlock.notify();
try {
util.mlock.wait();
} catch (Exception e) {
// TODO: handle exception
}
}
util.mlock.notify();
}
}
}
class util {
static final Object mlock = new Object();
}
Solution Explained: Before go into the explanation of each of solution i would like to remind some of the jargon here.
1. Join: So in multi threading environment multi threads always scheduled by a common scheduler which can not guarantee the execution of thread in terms of start and end of thread. but when we use join, it will provide the privilege of joining multi threads threads simultaneously.
public class JoinExample { public static void main(String[] args) { Thread th1 = new Thread(new MyClass(), "th1"); Thread th2 = new Thread(new MyClass(), "th2"); Thread th3 = new Thread(new MyClass(), "th3"); // Start first thread immediately th1.start(); /* Start second thread(th2) once first thread(th1) * is dead */ try { th1.join(); } catch (InterruptedException ie) { ie.printStackTrace(); } th2.start(); /* Start third thread(th3) once second thread(th2) * is dead */ try { th2.join(); } catch (InterruptedException ie) { ie.printStackTrace(); } th3.start(); // Displaying a message once third thread is dead try { th3.join(); } catch (InterruptedException ie) { ie.printStackTrace(); } System.out.println("All three threads have finished execution"); } } class MyClass implements Runnable{ @Override public void run() { Thread t = Thread.currentThread(); System.out.println("Thread started: "+t.getName()); try { Thread.sleep(4000); } catch (InterruptedException ie) { ie.printStackTrace(); } System.out.println("Thread ended: "+t.getName()); } }
No comments:
Post a Comment