This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Beginning Multithreading

 
Flo Powers
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Flo Powers
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic