-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ8.java
116 lines (95 loc) · 3.16 KB
/
Q8.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
/*Implemente um vetor (tamanho fixo) de inteiros seguro para threads com operações para ler e escrever em determinado índice e para trocar os valores armazenados em dois índices distintos.
Sua solução deve satisfazer as seguintes propriedades:
- Safety: duas threads nunca obtém acesso a uma posição do vetor ao mesmo tempo.
- Safety: a operação de troca (swap) dos valores em posições distintas deve ser atômica
- Liveness: para um vetor de tamanho N, até K <= N threads podem manipular elementos do vetor simultaneamente (considere que N é múltiplo de K).
Liveness: o programa não entra em deadlock*/
import java.util.concurrent.locks.*;
import java.util.Random;
public class Q8{
public static void main(String[] args){
int n = 10; //size of array
int coef = 2; //coef to garantee that k is multiple of n; Yep, there is the oposite in de text, but k <= n, so it's wrong consider n multiple of k
int k = n*coef; //number of threads
int[] array = new int[n];
Lock[] locks = new Lock[n];
ThreadArrayOperator[] threads = new ThreadArrayOperator[k];
//create locks
for(int i = 0; i < n; i++){
locks[i] = new ReentrantLock();
}
//create thraeds
for(int i = 0; i < k; i++){
threads[i] = new ThreadArrayOperator(i,array,locks);
threads[i].start();
}
}
}
class ThreadArrayOperator extends Thread{
int[] array;
Lock[] locks;
int id;
Random randGen = new Random();
public ThreadArrayOperator(int id, int[] array, Lock[] locks){
this.array = array;
this.locks = locks;
this.id = id;
}
public void run(){
while(true){
// System.out.println("Thread "+id+" attempted");8
//simulate array operations
double randomOperation = randGen.nextDouble();
if( randomOperation < 0.10){
//10% --> Swap
int index1 = randGen.nextInt(array.length);
int index2 = randGen.nextInt(array.length);
locks[index1].lock();
boolean l2 = locks[index2].tryLock();
if(!l2){
locks[index1].unlock();
}else{
try{
int temp1 = array[index1];
array[index1] = array[index2];
System.out.println("From thread: "+id+" SWAP "+index1+" and "+index2);
locks[index1].unlock();
array[index2] = temp1;
locks[index2].unlock();
}catch(Exception e){
locks[index1].unlock();
locks[index2].unlock();
e.printStackTrace();
}
}
}else if(randomOperation < 0.33){
//33% --> Write
int index = randGen.nextInt(array.length);
int value = randGen.nextInt(10000);
locks[index].lock();
try{
array[index] = value;
System.out.println("From thread: "+id+" WRITE "+value+" in "+index);
locks[index].unlock();
}catch(Exception e){
locks[index].unlock();
e.printStackTrace();
}
}else{
//33% --> Read
int index = randGen.nextInt(array.length);
locks[index].lock();
try{
int value = array[index];
System.out.println("From thread: "+id+" READ "+value+" from "+index);
locks[index].unlock();
}catch(Exception e){
locks[index].unlock();
e.printStackTrace();
}
}
// int simulateInterval = randGen.nextInt(50);
// sleep(simulateInterval);
}
}
}