Results 1 to 3 of 3
- 10-15-2012, 10:38 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 1
- Rep Power
- 0
print alternate char using two threads
Hello
I am new to threads. I have a problem with my program that should display A and B in alternate way using two threads. The output should be:
ABABABABAB
BUT,
I got different output in each run. ABAABBABA..... OR ABABBABB.....
This is my complete code:
Java Code:package altPackage; class A implements Runnable{ Thread aThread; public A() { aThread = new Thread(this,"A thread"); aThread.start(); } public void run() { for(int i=0; i<5 ; i++) { System.out.print("A"); try{ Thread.sleep(1000); } catch(InterruptedException e){ System.out.println("A thread interrupted."); } } } } class B implements Runnable{ Thread bThread; B() { bThread = new Thread(this,"B thread"); bThread.start(); } public void run() { for(int i=0; i<5 ; i++) { System.out.print("B"); try{ Thread.sleep(1000); } catch(InterruptedException e){ System.out.println("B thread interrupted."); } } } } public class altTest { public static void main(String[] args) { new A(); new B(); } }
- 11-17-2012, 07:42 PM #2
Member
- Join Date
- Nov 2012
- Posts
- 3
- Rep Power
- 0
Re: print alternate char using two threads
class node
{boolean isodd=true;
synchronized void prinodd()
{if(!isodd)
{try{wait();
}catch(InterruptedException e)
{System.out.print("exception caught");
}
}
isodd=false;
System.out.print("A");
notify();
}
synchronized void prineven()
{if(isodd)
{try{wait();
}catch(InterruptedException e)
{System.out.print("exception caught");
}
}
isodd=true;
System.out.print("B");
notify();
}
}
class odd implements Runnable
{node n1;
Thread t;
odd(node n1)
{this.n1=n1;
t=new Thread(this);
t.start();
}
public void run()
{while(true)
{try{t.sleep(100);} catch(Exception e){;}
n1.prinodd();
}
}
}
class even implements Runnable
{node n1;
Thread t;
even(node n1)
{this.n1=n1;
t=new Thread(this);
t.start();
}
public void run()
{while(true)
{n1.prineven();
}
}
}
class alt
{public static void main(String args[])
{System.out.println("press control +c to exit");
node n=new node();
odd a=new odd(n);
even b=new even(n);
try{a.t.join();
b.t.join();
}catch(InterruptedException e)
{System.out.println("exception caught");}
}
}
result
D:\>javac alt.java
D:\>java alt
press control +c to exit
ABABABABABABABABABABABABABABABABABABABABABABABABAB ABABABABABABABABABABABABABABAB
ABABABABABABABABABABABABABABABABABABABABABABABABAB ABABABABABABABABABABABABABABAB
ABABABABABABABABABABAB
D:\>
- 11-18-2012, 03:39 AM #3
Re: print alternate char using two threads
ankitb1989, you have been given two links in the thread you started. Kindly go through those links and edit your post accordingly.
If you continue to ignore responses and continue to post unformatted code, you may be banned for a period.
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
Similar Threads
-
How to print the integer value of char : '
By fatabass in forum New To JavaReplies: 2Last Post: 04-04-2012, 12:08 AM -
memory saving alternate to arrays
By boys21 in forum New To JavaReplies: 2Last Post: 10-11-2011, 01:20 PM -
replaceALL(char oldChar, char newChar) method
By arson09 in forum New To JavaReplies: 0Last Post: 04-28-2010, 06:48 AM -
Alternate Algorithm? Can you think of one?
By VinceGuad in forum New To JavaReplies: 8Last Post: 03-18-2009, 05:40 AM -
need help print char and digital
By lowpro in forum New To JavaReplies: 4Last Post: 12-04-2007, 08:20 AM
Bookmarks