-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreads.rb
65 lines (53 loc) · 1.45 KB
/
Threads.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Псевдо синхроное выполнение! Вуаля!
result = 0
thread1 = Thread.new do
sum = 0
1.upto(100) { |x|
sum = sum + x
sleep 0.00005
}
result += 1
puts "The sum of the first 10 integers is #{sum}"
end
thread2 = Thread.new do
product = 1
1.upto(100) {|x| product = product * x
sleep 0.00005
}
result += 1
puts "The product of the first 10 integers is #{product}"
end
puts "My threads is #{Thread.list}"
puts "And current threads is #{Thread.current} and the main is #{Thread.main}"
#Выполнение основанное на времени, как насчет выполнения основанного на пассе
until result == 2
thread1.join 0.0005
thread2.join 0.0005
end
result = 0
thread3 = Thread.fork do
sum = 0
1.upto(500) { |x|
sum += x #just summ this up
Thread.pass if x == 250
}
result += 1
puts "#{Thread.current}:Th3 is done."
end
thread4 = Thread.fork do
sum = 0
1.upto(500) { |x|
sum += x
Thread.pass if x == 250
}
puts "#{Thread.current}:Th4 is done."
result += 1
end
until result == 2
thread3.join
thread4.join
end
#
# С Паралелизмом вообщем то понятно
# Можно впринципе приминять для практически ничего в рельсах, но для вебсокетов
# Найти применение можно !