From b63cc32d916c4c71b23fc1a1a740787a6ecec927 Mon Sep 17 00:00:00 2001 From: Karthikeyan A K <77minds@gmail.com> Date: Thu, 18 Sep 2014 16:09:54 +0530 Subject: [PATCH] added programs to create many threads programatically --- many_threads.rb | 18 ++++++++++++++++++ many_threads_1.rb | 20 ++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 many_threads.rb create mode 100644 many_threads_1.rb diff --git a/many_threads.rb b/many_threads.rb new file mode 100644 index 0000000..355cc1f --- /dev/null +++ b/many_threads.rb @@ -0,0 +1,18 @@ +# many_threads.rb + +def launch_thread string + thread = Thread.new do + 3.times do + puts string + sleep rand(3) + end + end + return thread +end + +threads = [] +threads << launch_thread("Hi") +threads << launch_thread("Hello") + +threads.each {|t| t.join} + diff --git a/many_threads_1.rb b/many_threads_1.rb new file mode 100644 index 0000000..9576dca --- /dev/null +++ b/many_threads_1.rb @@ -0,0 +1,20 @@ +# many_threads_1.rb + +def launch_thread string + thread = Thread.new do + 3.times do + puts string + sleep rand(3) + end + end + return thread +end + +threads = [] + +4.times do |i| + threads << launch_thread("Thread #{i}") +end + +threads.each {|t| t.join} +