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

  • Home
  • SEARCH
  • 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 8985237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:17:15+00:00 2026-06-15T21:17:15+00:00

The idea is that the input will be provided by the console, and will

  • 0

The idea is that the input will be provided by the console, and will be identified with a unique ‘id’ as the first word of the input. A new thread is spawned when a new id is encountered, with the subsequent input being ‘start’. The thread should close when an input with the same id will say ‘close’. The ordering of the statements is random.

  • 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-06-15T21:17:16+00:00Added an answer on June 15, 2026 at 9:17 pm

    The trick here is to have a single thread (most easily the main thread) do all the reading. That thread will parse the command, start or stop threads, and dispatch commands to the threads. Each thread will have its own Queue from which it reads commands and into which the main thread puts the commands. Let’s see how it might be done.

    We’ll start with a little module for the control commands, so that they’re DRY:

    module ControlCommands
      START_COMMAND = 'start'
      STOP_COMMAND = 'stop'
    end
    

    Now let’s see the “main”:

    class Main
    
      def initialize
        @workers = Workers.new
        @console = Console.new(@workers)
      end
    
      def run
        @console.read_and_dispatch
        @workers.join
      end
    
    end
    
    Main.new.run
    

    There’s nothing much going on here. We make a console, and tell it to read commands and dispatch them to the workers. The console does not return from that until it has run out of input. The call to @workers.join make sure all the workers have finished working and properly shut down before the program exits.

    Here’s the Console class:

    class Console
    
      def initialize(workers)
        @workers = workers
      end
    
      def read_and_dispatch
        while input = gets
          @workers.dispatch *parse_input(input)
        end
      end
    
      private
    
      def parse_input(input)
        input.match(/^(\w+) *(.*)$/)[1..2]
      end
    
    end
    

    read_and_dispatch is the main loop. All that it is responsible for is reading and parsing input. As long as there is input, it splits it into the worker name and command, and then tells the workers to handle the command.

    Here’s the Workers class:

    class Workers
    
      include ControlCommands
    
      def initialize
        @workers = {}
      end
    
      def dispatch(worker_name, command)
        case command
        when START_COMMAND
          start_worker worker_name
        when STOP_COMMAND
          stop_worker worker_name
        else
          dispatch_worker worker_name, command
        end
      end
    
      def join
        @workers.each do |worker_name, worker|
          worker.stop
          worker.join
        end
      end
    
      private
    
      def start_worker(worker_name)
        @workers[worker_name] = Worker.new(worker_name)
      end
    
      def stop_worker(worker_name)
        @workers[worker_name].stop
      end
    
      def dispatch_worker(worker_name, command)
        @workers[worker_name].dispatch command
      end
    
    end
    

    This is where most of the meat is. This class creates workers (threads), stop them, and dispatches commands to them. Note that there is no error handling here: If you try to stop a thread that isn’t started, start one that’s already started, or dispatch a command to one that doesn’t exist, the program will crash or misbehave. I’ll leave the handling of those situations as an “exercise for the reader.”

    Here’s the Worker class:

    class Worker
    
      include ControlCommands
    
      def initialize(name)
        @name = name
        @commands = Queue.new
        @thread = start_thread
      end
    
      def dispatch(command)
        @commands.enq command
      end
    
      def stop
        @commands.enq STOP_COMMAND
      end
    
      def join
        @thread.join
      end
    
      private
    
      def start_thread
        Thread.new do
          loop do
            command = @commands.deq
            break if command == STOP_COMMAND
            process_command command
          end
        end
      end
    
      def process_command(command)
        print "thread #{@name} received command #{command.inspect}\n"
      end
    
    end
    

    This class contains the thread, and the queue used to communicate the between the main thread (the one reading the console) and the worker thread. That queue is also used to stop a thread, synchronously, by putting a STOP_COMMAND in the queue which the thread responds to by exiting. It is best, when you can afford to, to stop threads synchronously rather than asynchronously.

    Here’s a simple input file:

    foo start
    bar start
    foo input
    bar input
    foo stop
    bar stop
    

    And the output when the program is presented with that input:

    thread bar received command "input"
    thread foo received command "input"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What I want is a function I can run on user input that will
I have an input list that is being provided by a system that tracks
I had an idea that would require me be able to send and receive
I have some idea that it is due to some complex calculation, but i
I am playing with an idea that would allow people to add tickets to
I have always gone by the idea that casting should be avoided at all
For the last year or so I have followed the idea that if a
We have developed a set of crystal reports (not my idea) that are to
Im working on a small application to try out an idea that I have.
I'm totally willing to submit to the idea that my hardware is the cause

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.