Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #4306

Re: Creating and Executing New Threads

From 7stud -- <bbxx789_05ss@yahoo.com>
Newsgroups comp.lang.ruby
Subject Re: Creating and Executing New Threads
Date 2011-05-11 17:52 -0500
Organization Service de news de lacave.net
Message-ID <5e9dd109dd9aac91b7eafea052658d81@ruby-forum.com> (permalink)
References <2fad4e571f05e17ee09ba2e4b856b9f4@ruby-forum.com>

Show all headers | View raw


Dan King wrote in post #998095:
> I expected the script below to do the following:
>
> (1) spawn a new thread
> (2) connect to a server
> (3) read the data transmitted by the server
> (4) close the connection.
> (5) repeat 1-4
>
> While the script works, it does NOT spawn a new thread for each
> connection. Does anyone why that is the case? Thanks.
>


threads = []

10.times do |i|
  threads << Thread.new(i) do |thread_num|
    sleep(rand(3))
    puts thread_num
  end
end

threads.each { |t| t.join }

--output:--
1
2
3
6
5
4
9
0
7
8


Read the first paragraph here:

http://rubylearning.com/satishtalim/ruby_threads.html

-- 
Posted via http://www.ruby-forum.com/.

Back to comp.lang.ruby | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Creating and Executing New Threads Dan King <dan.king106@yahoo.com> - 2011-05-11 15:32 -0500
  Re: Creating and Executing New Threads Joel VanderWerf <joelvanderwerf@gmail.com> - 2011-05-11 15:44 -0500
  Re: Creating and Executing New Threads 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-11 17:52 -0500
    Re: Creating and Executing New Threads Christopher Dicely <cmdicely@gmail.com> - 2011-05-11 19:42 -0500
      Re: Creating and Executing New Threads 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-12 12:29 -0500

csiph-web