Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-09-2008, 03:49 PM
Member
 
Join Date: Sep 2008
Posts: 1
jodeen20 is on a distinguished road
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();
}
}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 09-10-2008, 03:03 PM
Member
 
Join Date: Sep 2008
Posts: 17
sagarbirari is on a distinguished road
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
__________________
Sagar Birari

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 09-10-2008, 03:46 PM
Niveditha's Avatar
Senior Member
 
Join Date: May 2008
Posts: 299
Niveditha is on a distinguished road
Send a message via Skype™ to Niveditha
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Over-riding purpose fails.. udayadas New To Java 7 08-24-2008 06:14 AM
Animation Synchronization dreadrocksean Advanced Java 5 08-08-2008 04:56 AM
synchronization question oguz Threads and Synchronization 2 07-22-2008 10:56 AM
Swing - catching click button event Java Tip Java Tips 0 03-12-2008 01:03 AM
AWT - catching click button event Java Tip Java Tips 0 03-12-2008 01:02 AM


All times are GMT +3. The time now is 07:59 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org