-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ7.java
164 lines (131 loc) · 3 KB
/
Q7.java
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*Modifique o programa do exercício anterior (Q6) para tornar mais fina a granularidade do travamento. Em outras palavras, faça com que o travamento seja feito por nó, ao invés de afetar a árvore inteira.
– Compare o desempenho desta versão com o da sequencial e o da que usa apenas uma trava*/
import java.util.Random;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Q7{
public static void main(String[] args){
Tree t = new Tree();
int nThreads = 10;
long millis = System.currentTimeMillis();
ThreadTree[] threads = new ThreadTree[nThreads];
for(int i = 0; i < nThreads ; i++){
threads[i] = new ThreadTree(i, t);
threads[i].start();
}
for(int i = 0; i < nThreads ; i++){
try{
threads[i].join();
}catch(Exception e){
e.printStackTrace();
System.out.println("Ops!Rolou um erro: "+e.getMessage());
}
}
int size = t.countNodes();
System.out.println(size);
long time = (System.currentTimeMillis() - millis);
System.out.println("Time: "+time);
}
}
class ThreadTree extends Thread{
int id;
Tree t;
int[] items;
Random randGen = new Random();
public ThreadTree(int id,Tree t){
this.id = id;
this.t = t;
}
public void run(){
for(int i = 0; i < 10000; i++){
int value = randGen.nextInt(10000);
t.insert(value);
}
}
public int countNodes(){
return t.countNodes();
}
}
class Tree{
Node root;
private Lock treeLock = new ReentrantLock();
public Tree(){
this.root = null;
}
public void insert(int value){
treeLock.lock();
//garantee only one root initialization
try{
if(this.root == null){
this.root = new Node(value);
System.out.println("Root created");
treeLock.unlock();
}else{
this.root.lock();
treeLock.unlock();
this.root.insert(value);
}
}catch(Exception e){
e.printStackTrace();
treeLock.unlock();
}
//try insert if root is not null
}
public int countNodes(){
if(root != null){
return this.root.countNodes();
}
return 0;
}
}
class Node{
public int value;
public Node left;
public Node right;
private Lock l = new ReentrantLock();
public Node(int value){
this.value = value;
this.left = null;
this.right = null;
}
public void lock(){
this.l.lock();
}
public void unlock(){
this.l.unlock();
}
public void insert(int value){
try{
if(value > this.value){
if(this.right == null){
this.right = new Node(value);
unlock();
}
else{
this.right.lock();
unlock();
this.right.insert(value);
}
}else if(value <= this.value){
if(this.left == null){
this.left = new Node(value);
unlock();
}
else{
this.left.lock();
unlock();
this.left.insert(value);
}
}//se for igual nao adiciona
}catch(Exception e){
e.printStackTrace();
unlock();
}
}
public int countNodes(){
int acc = 0;
if(this.left != null) acc += this.left.countNodes();
if(this.right != null) acc += this.right.countNodes();
return 1 + acc;
}
}