teste_thread_schedule.rb

Alexandre Riveira, 07/08/2014 09:56 AM

Download (955 Bytes)

 
1
#puts "definindo taskset -c -p 1 #{Process.pid}"
2
#`taskset -c -p 2 #{Process.pid}`
3

    
4
inicio_geral = Time.now
5
t1 = Thread.new do 
6
  Thread.current[:target] = 0
7
  while true
8
    Thread.current[:target] += 1 
9
  end
10
end
11

    
12
10.downto(0) do |i|
13
  inicio = Time.now
14
  Kernel.sleep(1)
15
  puts "faltando #{i} segundos (#{Time.now - inicio})"
16
end
17

    
18
puts "zerando thread com contagem em #{t1[:target]}"
19
t1[:target] = 0
20

    
21
t2 = Thread.new do
22
  Thread.current[:target] = 0
23
  while true
24
    Thread.current[:target] += 1
25
  end
26
end
27

    
28
t3 = Thread.new do
29
  Thread.current[:target] = 0
30
  while true
31
    Thread.current[:target] += 1
32
  end
33
end
34
10.times do
35
    puts "primeira #{t1[:target]}"
36
    puts "segunda  #{t2[:target]}"
37
    puts "terceira #{t3[:target]}"
38
    puts "\n\n"
39
    sleep(1)
40
end
41
total = t1[:target].to_i + t2[:target].to_i + t3[:target].to_i
42
puts "total #{total}"
43
puts "media #{total/(Time.now - inicio_geral).to_i}"
44
puts "\n\n"
45
puts "time #{Time.now - inicio_geral}"
46