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:
How to store thread variables so I can stop it without using database?
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?