Results 1 to 3 of 3
  1. #1
    jodeen20 is offline Member
    Join Date
    Sep 2008
    Posts
    1
    Rep Power
    0

    Default synchronization kid riding a swing n catching breath

    Hi java gurus im a newbie in jave have a problem with my java synchronized methods.. the output should be like this

    Alice: I want to ride the swing
    Alice: I get to ride the swing for 5940 ms
    Bob: I want to ride the swing
    Charlie: I want to ride the swing
    Alice: Im tired pausing for 4440 ms
    Bob: I get to ride the swing for 4231 ms
    Alice: I want to ride the swing
    Bob: Im tired pausing for 6934 ms
    Charlie: I get to ride the swing for 4204 ms
    ....

    this is my code.. any help would be greatly appreciated. Thanks! :)

    class MyRideSwing {
    public void rideswing(String name) {
    int mydelay = (int)(Math.random() * 5000);
    System.out.println(name + ": want");
    synchronized(this) {
    System.out.println(name + ": I get to ride the swing for " + mydelay +" milleseconds");
    try {
    Thread.sleep(mydelay);
    }catch (InterruptedException e){}
    this.breathe(name);
    }
    }
    public void breathe(String name) {
    int mydelay = (int)(Math.random() * 5000);
    System.out.println(name + ": tired" + mydelay +" milleseconds");
    try {
    Thread.sleep(mydelay);
    }catch (InterruptedException e){}
    }
    }

    class Kid extends Thread {
    String name;
    MyRideSwing p;
    Kid(String name, MyRideSwing p) {
    this.name = name; this.p = p;
    }
    public void run() {
    for (int ctr=0; ctr < 500; ctr++) {
    p.rideswing(name);
    }
    }

    }

    class RideASwing {
    public static void main(String args[]){
    MyRideSwing p = new MyRideSwing();
    Kid k1 = new Kid("Alice",p);
    Kid k2 = new Kid("Bob",p);
    Kid k3 = new Kid("Charlie",p);
    k1.start();
    k2.start();
    k3.start();
    }
    }

  2. #2
    sagarbirari is offline Member
    Join Date
    Sep 2008
    Posts
    17
    Rep Power
    0

    Default

    hello
    when u r create more than one thread and start it ,you cannot say that threads run in specific manner,JVM run thread with thread priority and scheduling algorithm which platform dependant.
    Hence order in which u want to print output is not possible

    Regards

  3. #3
    Niveditha's Avatar
    Niveditha is offline Senior Member
    Join Date
    May 2008
    Posts
    307
    Rep Power
    7

    Default

    Hi,
    Even i do go with the solution provided threads cant be managed to work in a specific order you want. Plz try some other alternative method for printing in a specific order you want other than threads.
    To finish sooner, take your own time....
    Nivedithaaaa

Similar Threads

  1. Over-riding purpose fails..
    By udayadas in forum New To Java
    Replies: 7
    Last Post: 08-24-2008, 05:14 AM
  2. Animation Synchronization
    By dreadrocksean in forum Advanced Java
    Replies: 5
    Last Post: 08-08-2008, 03:56 AM
  3. synchronization question
    By oguz in forum Threads and Synchronization
    Replies: 2
    Last Post: 07-22-2008, 09:56 AM
  4. Swing - catching click button event
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-12-2008, 12:03 AM
  5. AWT - catching click button event
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-12-2008, 12:02 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
  •