Results 1 to 4 of 4
  1. #1
    ankitb1989 is offline Member
    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Default print even and odd numbers using multithreads

    i want to print odd numbers and even numbers alternatively like
    odd:1
    even:2
    odd:3
    even:4
    .
    .

    .
    indefinitely


    where one thread prints odd number and another prints even number
    the even number thread waits for odd number thread and vice versa

    this code works fine

    class node
    {int counter=1;
    boolean isodd=true;

    synchronized void prinodd()
    {if(!isodd)
    {try{wait();
    }catch(InterruptedException e)
    {System.out.println("exception caught");
    }
    }
    isodd=false;
    System.out.println("odd: "+counter++);
    notify();

    }

    synchronized void prineven()
    {if(isodd)
    {try{wait();
    }catch(InterruptedException e)
    {System.out.println("exception caught");
    }
    }
    isodd=true;
    System.out.println("even: "+counter++);
    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 eo
    {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");}


    }
    }


    this code works correctly and i am able to do so


    but now this next version of code does not work and gives error
    error

    press control +c to exit
    odd: 1
    Exception in thread "Thread-0" odd: 2
    Exception in thread "Thread-1" java.lang.IllegalMonitorStateException
    at java.lang.Object.notify(Native Method)
    at odd.run(eo1.java:22)
    at java.lang.Thread.run(Thread.java:722)
    java.lang.IllegalMonitorStateException
    at java.lang.Object.notify(Native Method)
    at even.run(eo1.java:46)
    at java.lang.Thread.run(Thread.java:722)

    code



    class node
    {static int counter=1;
    static boolean isodd=true;
    }

    class odd extends node implements Runnable
    {Thread t;
    odd()
    {t=new Thread(this);
    t.start();
    }

    public void run()
    {while(true)
    {if(!isodd)
    {try{wait();}
    catch(InterruptedException e)
    {System.out.println("exception caught");}
    }
    isodd=false;
    System.out.println("odd: "+counter++);
    notify();

    }

    }
    }


    class even extends node implements Runnable
    {Thread t;
    even()
    {t=new Thread(this);
    t.start();
    }

    public void run()
    {while(true)
    {if(isodd)
    {try{wait();}
    catch(InterruptedException e)
    {System.out.println("exception caught");}
    }
    isodd=true;
    System.out.println("odd: "+counter++);
    notify();

    }

    }

    }

    class eo1
    {public static void main(String args[])
    {System.out.println("press control +c to exit");

    odd a=new odd();
    even b=new even();


    try{a.t.join();
    b.t.join();
    }catch(InterruptedException e)
    {System.out.println("exception caught");}


    }
    }


    please tell me what is the error according to me this should work

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

    Default Re: print even and odd numbers using multithreads

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

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

    Default Re: print even and odd numbers using multithreads

    what do u mean db

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

    Default Re: print even and odd numbers using multithreads

    Quote Originally Posted by ankitb1989 View Post
    what do u mean db
    Go through the links, and also this.

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

Similar Threads

  1. Print Numbers with a loop from an array
    By ejdrijin in forum New To Java
    Replies: 1
    Last Post: 05-30-2012, 08:46 PM
  2. How do I print numbers in a line?
    By Emtino in forum New To Java
    Replies: 2
    Last Post: 04-17-2012, 09:08 AM
  3. Replies: 12
    Last Post: 03-28-2011, 10:30 PM
  4. Replies: 6
    Last Post: 12-08-2010, 01:39 AM
  5. print random numbers without repetition
    By princess.blue in forum New To Java
    Replies: 3
    Last Post: 02-04-2010, 10:37 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
  •