wm: irc-h2o-bot

ref: 5c482500316fb1964e64db9c42322b839dfc0606
dir: /Test_Multi_Threading.rb/

View raw version
require 'thread'
require 'concurrent'

executor = Concurrent::ThreadPoolExecutor.new(
  min_threads: 2,
  max_threads: 5,
  max_queue: 10
)

futures = []

5.times do |i|
  futures << Concurrent::Future.execute(executor: executor) do
    puts "Async task #{i} started"
    sleep(rand(1..5))
    puts "Async task #{i} finished"
  end
end

futures.each(&:wait)
executor.shutdown

# 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