-
Notifications
You must be signed in to change notification settings - Fork 0
/
EJ1.java
45 lines (31 loc) · 928 Bytes
/
EJ1.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
public class EJ1 {
public static class MultiThread extends Thread{
public long id;
MultiThread(long id){
this.id = id;
}
public void run() {
System.out.println("El proceso " + id + " se esta ejecutando");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("El proceso " + id + " ha terminado");
}
}
public static void main(String[] args) throws InterruptedException {
int N = 5;
MultiThread p[] = new MultiThread[N];
//int T[] = {100, 200, 50, 80, 10};
for(int i = 0; i < N; i++) {
p[i] = new MultiThread(N*5 + i);
p[i].start(); // Se puede poner en otro for
}
// Espera a que los hilos terminen
for(int i = 0; i < N; i++) {
p[i].join();
}
System.out.println("Los " + N + " procesos han terminado");
}
}