Ruby Multithreading

Multithreading is a way to work concurrence execute of multiple process using same resources. That is mainly used to long-running tasks in background process. The main goal of multithreading are use maximum power of system resources and providing fast execution. You'll are apply multithreading concept in ruby programming by Thread class.

# Ruby Thread Execution 
def test_one 
    operation = 0
    while operation <= 4
      
      puts "Test One : #{operation}"
      operation+=2
      sleep(1) #specified time 
    end
  
end
  
# Second method 
def test_two 
   operation = 0
    while operation <= 4
  
      puts "Test Two : #{operation}"
      operation+=1
      sleep(0.7) #specified time 

    end
  
end
puts "Process Start" 
# creating thread for test_one method 
stateOne = Thread.new{test_one()} 
  
# creating thread for test_two method 
stateTwo= Thread.new{test_two()} 
  

stateOne.join 
  

stateTwo.join 
  
  
puts "Process End"
Process Start
Test One : 0
Test Two : 0
Test Two : 1
Test One : 2
Test Two : 2
Test One : 4
Test Two : 3
Test Two : 4
Process End


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved