-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path006_threads_matrix.rb
More file actions
executable file
·79 lines (63 loc) · 1.6 KB
/
006_threads_matrix.rb
File metadata and controls
executable file
·79 lines (63 loc) · 1.6 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
def Out_Array(a1, show, size)
puts
0.upto(show){ |i|
0.upto(show){ |j| print a1[i][j],"\t"}
print ". . .\t"
(size-show).upto(size){ |j| print a1[i][j],"\t"}
puts
}
puts ". . ." , ". . ." , ". . ."
(size-show).upto(size){ |i|
0.upto(show){ |j| print a1[i][j],"\t"}
print ". . .\t"
(size-show).upto(size){ |j| print a1[i][j],"\t"}
puts
}
puts
end
size = 20
show = 2
thread_num = 8
threads = []
puts "\n"
#List Lines
linhas = []
thread_num.times{ |i|
linhas[i] = Array.new
linhas[i][0] = i*size/thread_num
linhas[i][0] = linhas[i-1][0] + size/thread_num + 1 if i != 0
linhas[i][1] = linhas[i][0] + size/thread_num
linhas[i][1] = size if i == thread_num-1
}
#Show Lines
puts "","Table:"
linhas.each{ |a|
puts "S\t#{a[0]}\t...\tF\t#{a[1]}"
}
#==========================================================================================
#Matrix Fill
a1 = []
for i in 0..size
a1[i] = Array.new
for j in 0..size
a1[i][j] = (rand*size).to_int
end
end
puts "\nRandom:"
Out_Array(a1, show, size)
#==========================================================================================
#Threads List
thread_num.times{ |i|
threads[i] = Thread.new(){
for j in linhas[i][0]..linhas[i][1]
for k in 0..size
for l in 0..size
a1[j][l] , a1[j][k] = a1[j][k] , a1[j][l] if a1[j][l] >= a1[j][k]
end
end
end
}
}
threads.each {|t| t.join }
puts "Sort:"
Out_Array(a1, show, size)