Results 1 to 3 of 3
  1. #1
    confident is offline Member
    Join Date
    Oct 2012
    Posts
    1
    Rep Power
    0

    Default 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();
    	}
    }
    Thanks a lot in advance

  2. #2
    ankitb1989 is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default 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:\>

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,304
    Rep Power
    20

    Default 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.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. How to print the integer value of char : '
    By fatabass in forum New To Java
    Replies: 2
    Last Post: 04-03-2012, 11:08 PM
  2. memory saving alternate to arrays
    By boys21 in forum New To Java
    Replies: 2
    Last Post: 10-11-2011, 12:20 PM
  3. replaceALL(char oldChar, char newChar) method
    By arson09 in forum New To Java
    Replies: 0
    Last Post: 04-28-2010, 05:48 AM
  4. Alternate Algorithm? Can you think of one?
    By VinceGuad in forum New To Java
    Replies: 8
    Last Post: 03-18-2009, 04:40 AM
  5. need help print char and digital
    By lowpro in forum New To Java
    Replies: 4
    Last Post: 12-04-2007, 07:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •