# Starting Threads

**URL:** <https://www.ruby-forum.com/t/starting-threads/118311>\
**Category:** Ruby\
**Created:** [October 25, 2007, 3:56pm UTC](https://www.ruby-forum.com/t/starting-threads/118311 "2007-10-25T15:56:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![flnative](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5daacb/32.png) [@flnative](https://www.ruby-forum.com/u/flnative)\
**Post date:** [October 25, 2007, 3:56pm UTC](https://www.ruby-forum.com/t/starting-threads/118311/1 "2007-10-25T15:56:20Z")

</div>

Hi @all  
I’ve many different threads to run parallel. Some of these have a  
special feature. At the starttime, I decide with a variable, which  
threads must sleep for 5 ms. The others can start now.

How can I realize this problem??

thanks…

---

<div class="post-metadata">

**Author:** ![flnative](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5daacb/32.png) [@flnative](https://www.ruby-forum.com/u/flnative)\
**Post date:** [October 25, 2007, 5:22pm UTC](https://www.ruby-forum.com/t/starting-threads/118311/2 "2007-10-25T17:22:13Z")

</div>

K. R. wrote:

> Hi @all  
> I’ve many different threads to run parallel. Some of these have a  
> special feature. At the starttime, I decide with a variable, which  
> threads must sleep for 5 ms. The others can start now.
> 
> How can I realize this problem??
> 
> thanks…

Thread.new # Look up some uses for it based on what your doing.

as for the variable… wait = sleep(5)

# i assume you meant 5 secs and not 5 milliseconds because i believe

that you cant sleep under 1 second if i remember correctly as sleep does  
not allow sleep(0.05)

---

<div class="post-metadata">

**Author:** ![flnative](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5daacb/32.png) [@flnative](https://www.ruby-forum.com/u/flnative)\
**Post date:** [October 25, 2007, 6:55pm UTC](https://www.ruby-forum.com/t/starting-threads/118311/3 "2007-10-25T18:55:27Z")

</div>

K. R. wrote:

> Hi @all  
> I’ve many different threads to run parallel. Some of these have a  
> special feature. At the starttime, I decide with a variable, which  
> threads must sleep for 5 ms. The others can start now.
> 
> How can I realize this problem??
> 
> thanks…

Try something like this:

x = 1  
threads = []

10.times do |i|  
threads \<\< Thread.new(x) do |my\_var|  
if my\_var \< 5  
my\_var = 10  
sleep(0.05)  
end

```
puts "thread #{i} executing"

```

end  
end

threads.each do |thr|  
thr.join  
end

–output:–  
thread 4 executing  
thread 3 executing  
thread 2 executing  
thread 1 executing  
thread 0 executing  
thread 9 executing  
thread 8 executing  
thread 7 executing  
thread 6 executing  
thread 5 executing

---

<div class="post-metadata">

**Author:** ![flnative](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5daacb/32.png) [@flnative](https://www.ruby-forum.com/u/flnative)\
**Post date:** [October 25, 2007, 9:41pm UTC](https://www.ruby-forum.com/t/starting-threads/118311/4 "2007-10-25T21:41:43Z")

</div>

On Oct 25, 2007, at 7:56 AM, K. R. wrote:

> Hi @all  
> I’ve many different threads to run parallel. Some of these have a  
> special feature. At the starttime, I decide with a variable, which  
> threads must sleep for 5 ms. The others can start now.
> 
> How can I realize this problem??

threads aren’t quite that deterministic - you may find some without  
sleep take longer to start that the ones with the 5ms sleep. what  
are you trying to do that requires such strict timing constraints?

regards.

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![flnative](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5daacb/32.png) [@flnative](https://www.ruby-forum.com/u/flnative)\
**Post date:** [October 25, 2007, 7:42pm UTC](https://www.ruby-forum.com/t/starting-threads/118311/5 "2007-10-25T19:42:55Z")

</div>

Michael L. wrote:  
…

> that you cant sleep under 1 second if i remember correctly as sleep does  
> not allow sleep(0.05)

It does: ruby’s #sleep calls select() in that case.
