-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilosofo.java
More file actions
56 lines (53 loc) · 1.26 KB
/
filosofo.java
File metadata and controls
56 lines (53 loc) · 1.26 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
package comensales;
import java.util.Random;
public class filosofo implements Runnable {
int id;
Thread t;
tenedor derecho;
tenedor izquierdo;
int espera;
Random r;
tenedor mesa[];
filosofo(int id,tenedor derecho, tenedor izquierdo,tenedor mesa[]){
this.id = id;
this.derecho = derecho;
this.izquierdo = izquierdo;
this.mesa = mesa;
r = new Random();
t = new Thread(this);
t.start();
}
public void run(){
for(int i =0;i<1;i++){
synchronized(this.derecho){
synchronized(this.izquierdo){
comer();
}
}
pensar();
}
System.out.println("Filosofo "+id+" termino ");
}
void comer(){
System.out.println("\tFilosofo "+id+ " comienza a comer tomar tenedores "+ this.izquierdo.id+"y"+this.derecho.id);
derecho.tomar(id);
izquierdo.tomar(id);
espera = r.nextInt(5000);
System.out.println("\tFilosofo "+id+ " comiendo...");
try{
Thread.sleep(espera);
}catch(InterruptedException e){
}
derecho.soltar();
izquierdo.soltar();
System.out.println("\tFilosofo "+id+ " termino comenzara a pensar ");
}
void pensar(){
espera = (r.nextInt(5000));
try{
System.out.println("\tFilosofo " + id+ " piensa....");
Thread.sleep(espera);
}catch(InterruptedException e){
}
}
}