aspose file tools*
The moose likes Beginning Java and the fly likes Beginning Multithreading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Beginning Multithreading" Watch "Beginning Multithreading" New topic
Author

Beginning Multithreading

Flo Powers
Ranch Hand

Joined: May 12, 2004
Posts: 57
Hi, I am trying to work out how to work with multithreading. I'm guessing that's what's needed. To experiment, I've set myself a little challenge, but I get the feeling I may be trying to go about it the wrong way.

Here's what I'm trying to make: a game-like environment where there's a world object, with instance variables hour, minute and description. The description is a simple String and changes both with the time (day/night) and randomized weather (every hour or so, it may go cloudy or sunny, and during the night, there may or may not be a moon out). This class extends Thread, and I am using the sleep( ) method with an argument of 3000 milliseconds to make time go by and let the weather change. One minute in the real world is one hour in this world object. When I create a World object, and watch it tick along, I can see that it works. Inside the constructor is a loop that outputs the time and the description.

The problem: I also want to have a player object that can do things. I have created another class that inputs user commands (for now, just "Look", "Time" and "Quit". However, I can't make both things work. When I create a World( ) object it never waits for me to do input. I guess this is because the constructor in the World object loops forever. If I try to put the user input as part of this loop, it interrupts the thread, as it pauses while waiting for user input.

What I would like: a World object where time passes and the weather changes, and at the same time the ability to have user input (when the user wants) of commands and have the world object return Time or description.

If you see a simple logic flaw and could give a pointer in the right direction, I'd be mighty grateful.

Thanks!
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Flo Powers:
...When I create a World( ) object it never waits for me to do input. I guess this is because the constructor in the World object loops forever....

It sounds like this is exactly what's happening. You need your constructor to exit, leaving you with an object that's ready to go. And since this object is a Thread, it sounds like the loop belongs in the overridden run() method rather than the constructor.

Once you have your instance created, you need to call start() on it. This is the key, because that's what allows it to execute as a separate thread.

See this thread tutorial.
[ March 03, 2006: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Flo Powers
Ranch Hand

Joined: May 12, 2004
Posts: 57
Thanks, I'll experiment with that. I also decided I probably need to create a console with which to interact with this World object - commands are entered in a JTextField, and when the user presses enter, it commits the command. What I was trying before was a loop that inputs user commands in a program running in the command prompt. I think this made the program wait for user input.

Thanks! Will ask again if (when) I get stuck again (and cannot find answers in the thread you provided a link to).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Beginning Multithreading