• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Beginning Multithreading

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
When evil is afoot and you don't have any arms you gotta be hip and do the legwork, but always kick some ... tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic