-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path20150630.java
More file actions
185 lines (145 loc) · 3.15 KB
/
20150630.java
File metadata and controls
185 lines (145 loc) · 3.15 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//com gambiarras absurdas, não leia... fora mais de 3h de tentativa a ideia *brilhante*
//trabalho 13
import java.util.concurrent.locks.ReentrantLock;
public class trabalho13{
static Ficha f = new Ficha(0);
static int numThreads = 8;
static int contador = 0;
public static void main(String[] args){
Thread[] t = new Util[numThreads];
do{
for (int i = 0; i < numThreads; i++) {
t[i] = new Util(i);
t[i].start();
}
for (int i = 0; i < numThreads; i++) {
try{
t[i].join();
} catch(Exception e){}
}
contador++;
} while(contador < 10000);
}
static class Util extends Thread{
int threadId;
int fichaPorThread = 10;
int numFicha, fichaMin;
String name;
public Util(int n){
threadId = n;
name = "t" + threadId;
}
public void run(){
synchronized(f){
f.getFicha();
System.out.println("Thread " + threadId + " Turn " + turn + " Ficha " + f.ficha);
}
}
}
}
class Ficha{
int ficha;
ReentrantLock locker;
public Ficha(int i){
ficha = i;
locker = new ReentrantLock();
}
public int getFicha(){
return ficha++;
}
public void goOn(){
//turn++;
//action.unlock();
}
}
/*
*
* 2.
* Safety é
* Liveness é o oposto do deadlock, ao invés de segurar o recurso existe uma facilidade de soltar
* o recurso para que outra thread o use, porém num cenário que se prolonga temporalmente termina
* que nenhum trabalho é realmente feito. (Filósofos educados)
*
* Problema das fichas
*
*
*/
//Exercicios da aula 14
import java.util.concurrent.locks.*;
public class VetorThreadSafe{
static int[] vetor = new int [3];
static Lock locker = new ReentrantLock();
public int read(int pos){
return vetor[pos];
}
public void write(int pos, int val){
locker.lock();
try{
vetor[pos] = val;
} finally{
locker.unlock();
}
}
public void swap(int pos1, int pos2, VetorThreadSafe vts){
Boolean l1 = locker.tryLock();
Boolean l2 = vts.locker.tryLock();
int aux;
try{
while(!(l1 && l2)){
if(!l1) locker.unlock();
if(!l2) vts.locker.unlock();
l1 = locker.tryLock();
l2 = vts.locker.tryLock();
}
aux = vetor[pos1];
vetor[pos1] = vetor[pos2];
vetor[pos2] = aux;
} finally{
locker.unlock();
vts.locker.unlock();
}
}
public static void main(String[] args) {
VetorThreadSafe v = new VetorThreadSafe();
Thread pr = new Produtor(v);
Thread cns = new Consumidor(v);
pr.start();
cns.start();
}
}
class Produtor extends Thread{
VetorThreadSafe vts;
public Produtor(VetorThreadSafe v){
vts = v;
}
public void run(){
for (int i = 0; i < 3; i++) {
for (int j = 1; j < 5; j++) {
vts.write(i, j+5);
System.out.print("write ");
for(int k = 0; k < 3; k++){
System.out.print(vts.read(i) + " ");
}
System.out.println();
}
}
}
}
class Consumidor extends Thread{
VetorThreadSafe vts;
public Consumidor(VetorThreadSafe v){
vts = v;
}
public void run(){
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
vts.swap(i, j, vts);
System.out.print("Swap ");
for(int k = 0; k < 3; k++){
System.out.print(vts.read(i) + " ");
}
System.out.println();
}
}
}
}