-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ4.java
56 lines (48 loc) · 1.21 KB
/
Q4.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
/*
Modifique o último programa que você construiu para que, a cada iteração, a thread espere 1ms. A thread que terminar a contagem primeiro (realizar todas as iterações) deve interromper todas as outras que estão executando.*/
public class Q4{
public static void main(String[] args){
int n = 100;
int counter = 0;
int xThreads = 4;
ThreadCounter[] threads = new ThreadCounter[xThreads];
for(int i =0; i < xThreads ; i++){
threads[i] = new ThreadCounter(i,n,counter);
threads[i].start();
}
while(true){
for(int i =0; i < xThreads ; i++){
if(threads[i].finished){
for(int j =0; j < xThreads ; j++){
if(j != i) threads[i].interrupt();
}
return;
}
}
}
}
}
class ThreadCounter extends Thread{
private int id;
private int max;
private int counter;
public boolean finished;
public ThreadCounter(int id, int max,int counter){
this.id = id;
this.max = max;
this.counter = counter;
this.finished = false;
}
public void run(){
while(counter < max){
counter++;
System.out.println("From "+id+": "+counter);
try{
this.sleep(1);
}catch(Exception e){
System.out.println("Ops! Houve um erro: "+e.getMessage());
}
}
finished = true;
}
}