Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 831511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:12:58+00:00 2026-05-15T04:12:58+00:00

I am trying to program a game in which I have a Table class

  • 0

I am trying to program a game in which I have a Table class and each person sitting at the table is a separate thread. The game involves the people passing tokens around and then stopping when the party chime sounds.

how do i program the run() method so that once I start the person threads, they do not die and are alive until the end of the game

One solution that I tried was having a while (true) {} loop in the run() method but that increases my CPU utilization to around 60-70 percent. Is there a better method?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T04:12:59+00:00Added an answer on May 15, 2026 at 4:12 am

    While yes, you need a loop (while is only one way, but it is simplest) you also need to put something inside the loop that waits for things to happen and responds to them. You’re aiming to have something like this pseudocode:

    loop {
        event = WaitForEvent();
        RespondToEvent(event);
    } until done;
    

    OK, that’s the view from 40,000 feet (where everything looks like ants!) but it’s still the core of what you want. Oh, and you also need something to fire off the first event that starts the game, obviously.

    So, the key then becomes the definition of WaitForEvent(). The classic there is to use a queue to hold the events, and to make blocking reads from the queue so that things wait until something else puts an event in the queue. This is really a Concurrency-101 data-structure, but an ArrayBlockingQueue is already defined correctly and so is what I’d use in my first implementation. You’ll probably want to hide its use inside a subclass of Thread, perhaps like this:

    public abstract class EventHandlingThread<Event> extends Thread {
        private ArrayBlockingQueue<Event> queue = new ArrayBlockingQueue<Event>();
        private boolean done;
    
        protected abstract void respondToEvent(Event event);
        public final void postEvent(Event event) throws InterruptedException {
            queue.put(event);
        }
        protected final void done() {
            done = true;
        }
        public final void run() {
            try {
                while (!done) {
                    respondToEvent(queue.take());
                }
            } catch (InterruptedException e) {
                // Maybe log this, maybe not...
            } catch (RuntimeException e) {
                // Probably should log this!
            }
        }
    }
    

    Subclass that for each of your tasks and you should be able to get going nicely. The postEvent() method is called by other threads to send messages in, and you call done() on yourself when you’ve decided enough is enough. You should also make sure that you’ve always got some event that can be sent in which terminates things so that you can quit the game…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a program in C++ which runs Conway's Game of Life.
I'm trying to make my first game, a console tetris. I have a class
I am trying to write a program to simulate a card game. I have
I am writing a game engine/library in which I have an event dispatcher class
I'm trying to program my first GUI-class in Java, using the Window Builder. Ok,
I'm trying to program a graph class using an adjacent list from an example
I'm trying to program an Android interface which uses an expandable list on the
I am trying to do the design of a Bejeweled game. I have basically
Im making a program about the game Go and im trying to write some
I am trying to write a program which will allow two players to play

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.