3

Currently I have some code like this:

class WorkingWithThread
  def self.start_first_thread
    @@first_thread = Thread.new do
      do something
      repeat # infinitive loop
    end
  end
  def self.start_second_thread
    @@second_thread = Thread.new do
      do something
      repeat # infinitive loop
    end
  end

  def self.stop_thread
    begin
      Thread.kill @@first_thread
      Thread.kill @@second_thread
      p 'STOP THREAD'
    rescue Exception => e
      p 'NO THREAD OPENING'
    end
  end
end

Max threads pool is 3, timeout = 600 secs. I made 2 api requests to start and stop threads.

It works properly. However, if the start api is called 2 times, the variables @@first_thread and @@second_thread seems to be reinitialized and those running threads can't be killed. In addition, after a while, sometimes, the stop api also doesn't works even when I call start api one time only (i need explanation here).

The questions is:

  1. How to store thread variables so I can stop it without using database?

  2. I'm thinking about adding method to block start api when there are running threads? Is it viable? If it is, how to stop those threads when it happens as unexplained reason that i mentioned above?

1 Answer 1

1

Why not instantiate an instance of the class and store the running threads there? For example:

class WorkingWithThread
  attr_accessor :first_thread, :second_thread

  def run
    start_first_thread
    start_second_thread
  end

  def start_first_thread
    return puts("First thread already running") if self.first_thread

    puts "Launching thread one!"
    self.first_thread = Thread.new do
      # do something
      repeat # infinite loop
    end
  end

  def start_second_thread
    return puts("Second thread already running") if self.second_thread

    puts "Launching thread two!"
    self.second_thread = Thread.new do
      # do something
      repeat # infinite loop
    end
  end

  def stop_threads
    begin
      Thread.kill first_thread if first_thread
      Thread.kill second_thread if second_thread
      p 'STOP THREADS'
    rescue Exception => e
      p 'NO THREADS OPENING'
    end
  end
end

Then to use it:

worker = WorkingWithThread.new
worker.run
# => Launching thread one!
# => Launching thread two!

worker.run
# => First thread already running
# => Second thread already running

worker.stop_threads
# => STOP THREADS

That way you'll have access to the running threads throughout - let me know if that helps and how you get on :)

Sign up to request clarification or add additional context in comments.

4 Comments

How to call those two separate API to call that worker, should I use class variable again? Your method of using attr_accessor might come in handy, thanks.
I think the bottom section should cover that @HùngNguyễn - calling the run method on an instance will run both, or you can call them separately if needed. How has this helped out? Suitable to accept as the answer here?
@HùngNguyễn why was this unaccepted as the answer? :(
It's easy to use it as you said, so I upvoted. But the question involved using API to call the method, when using another API, it still does not know what worker to stop, sometimes (I have no idea why)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.