Saturday, 14 September 2013

Print Natural Sequence with help of 2 threads(1 is printing even and 2'nd is printing odd)

Print Natural Sequence with help of 2 threads(1 is printing even and 2'nd
is printing odd)

I have tired this question, and i ended up with some doubts. Please help
me out
Doubt : If any thread is in wait state , and no other thread is notifying
that one , so will it never come to and end ? Even after using wait(long
milliseconds).
For Code : What my requirement is from the code(Please Refer My Code) :
a : Should print "Even Thread Finish " and "Odd Thread Finish" (Order is
not imp , but must print both)
b: Also in main function should print " Exit Main Thread"
What is actually happening : After lot of runs , in some cases , it prints
"Even Thread Finish" then hangs here or vice-versa. In some cases it
prints both.
Also it never prints "Exit Main Thread".
So How to modify code , so it must print all 3 statement .(Of Course "Exit
Main.. " in last , as i am using join for main.)
In brief : Main start-> t1 start -> t2 start ,, then i need t2/t1 finish
-> main finish.
Please help me out for this problem



Here is my code :



import javax.sql.CommonDataSource;
public class ThreadTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Share commonObj = new Share();
Thread even = new Thread(new EvenThread(commonObj));
Thread odd = new Thread(new OddThread(commonObj));
even.start();
odd.start();
try {
Thread.currentThread().join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Exit Main Thread");
}
}
class EvenThread implements Runnable{
private Share commShare;
public EvenThread(Share obj) {
// TODO Auto-generated constructor stub
this.commShare = obj;
}
private int number = 2;
public void run() {
System.out.println("Even Thread start");
while(number <= 50){
if(commShare.flag == true){
System.out.println("Even Thread" + number);
number+=2;
commShare.flag = false;
synchronized (commShare) {
try {
commShare.notify();
commShare.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
commShare.notify();
}
}else{
synchronized (commShare) {
try {
commShare.notify();
commShare.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
commShare.notify();
}
}
}
System.out.println("Even Thread Finish");
}
}
class OddThread implements Runnable{
private int number = 1;
private Share commShare;
public OddThread(Share obj) {
// TODO Auto-generated constructor stub
this.commShare = obj;
}
public void run() {
System.out.println("Odd Thread start");
while(number <=50 ){
if(commShare.flag == false){
System.out.println("Odd Thread :" + number);
number+=2;
commShare.flag = true;
synchronized (commShare) {
try {
commShare.notify();
commShare.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
commShare.notify();
}
}
}
System.out.println("Odd Thread Finish");
}
}
class Share {
Share sharedObj ;
public boolean flag = false;
}

No comments:

Post a Comment