-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ5.java
146 lines (122 loc) · 2.89 KB
/
Q5.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
/*
Construa uma classe que implementa uma fila segura para um número indeterminado de threads que funciona da seguinte maneira:
– Se apenas uma thread tentar inserir ou remover um elemento, ela consegue
– Se mais que uma estiver tentando ao mesmo tempo, uma consegue e as outras esperam. A próxima só conseguirá realizar a operação quando a anterior tiver terminado.
*/
import java.util.Random;
public class Q5{
public static void main(String[] args){
Queue q = new Queue();
int nThreads = 4;
int itemsPerThread = 10;
Random randGen = new Random();
//Class operation for simulate some dynamic activities over the queue
Operation[][] ops = new Operation[nThreads][itemsPerThread];
//Setting up simulation
for(int i = 0; i < nThreads;i++){
for(int j = 0; j < itemsPerThread ; j++){
ops[i][j] = new Operation();
if(randGen.nextDouble() > 0.7) ops[i][j].setGet();
else ops[i][j].setPut((i*10)+j+1); //Each item is generated sorted
}
}
for(int i = 0; i < nThreads ; i++){
(new ThreadQueuer(i+1,q,ops[i])).start();
}
}
}
class Operation{
public int e;
public String op;
public Operation(){
this.e = -1;
}
public void setPut(int e){
this.e = e;
this.op = "put";
}
public void setGet(){
this.e = -1;
this.op = "get";
}
}
class Queue{
public Node first;
public Queue(){
}
synchronized public void put(int e){
if(first != null){
first = (new Node(e)).next = first;
}else{
first = new Node(e);
}
}
synchronized public void printQueue(){
printQueue(this.first);
}
synchronized private void printQueue(Node n){
if(n != null){
System.out.print(n.e);
printQueue(n.next);
if(n.next != null) System.out.print(',');
}else{
System.out.println();
}
}
synchronized public int get() throws Exception{
if(first == null){
throw (new Exception("A fila ja acabou camarada"));
}
int v = first.e;
first = first.next;
return v;
}
}
class Node{
public int e;
public Node next;
public Node(int e){
this.e = e;
}
}
class ThreadQueuer extends Thread{
private Queue q;
private Operation[] ops;
private int id;
public ThreadQueuer(int id, Queue q, Operation[] ops){
this.id = id;
this.q = q;
this.ops = ops;
}
public void run(){
for(int i = 0; i < ops.length ; i++ ){
try{
execute(ops[i]);
}catch(Exception e){
System.out.println("Ops! Houve um erro: "+e.getMessage());
}
}
}
private void execute(Operation op) throws Exception{
if( op.op == "put"){
this.put(op.e);
}else if(op.op == "get"){
this.get();
}else{
throw (new Exception("Operation Type Invalid For Queue"));
}
// q.printQueue();
}
private void put(int e){
q.put(e);
System.out.println("From: "+id+" -> "+"put: "+e);
}
private void get(){
try{
int v = q.get();
System.out.println("From: "+id+" -> "+"get: "+v);
}catch(Exception e){
System.out.println("Ops! Houve um erro: "+e.getMessage());
}
}
}