wm: irc-h2o-bot

ref: fba8902621f2de389bb3af4aa55e3c0cc87e5871
dir: /Test_Multi_Threading.rb/

View raw version
require 'thread'

# Define a method that performs some asynchronous task
def async_task(name)
  puts "Starting #{name}"

  # Simulate some work
  sleep(rand(5))

  puts "#{name} completed"
end

# Create multiple threads for asynchronous execution
threads = []
threads << Thread.new { async_task('Task 1') }
threads << Thread.new { async_task('Task 2') }
threads << Thread.new { async_task('Task 3') }

# Wait for all threads to complete
threads.each(&:join)

puts "All tasks completed"



def Test
  for i in 0..10 do
    puts i
    sleep(rand(0.1..1.0));
  end
end

t1 = Thread.new{Test()}
t2 = Thread.new{Test()}
puts 'moz'
t1.join

t2.join