Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Random;
- public class Consumidor extends Thread{
- ArrayList<Integer> lista;
- int indice;
- public Consumidor(ArrayList<Integer> lista, int i) {
- this.lista = lista;
- this.indice = i;
- }
- public void run() {
- Random random = new Random();
- int consumido;
- int aux;
- aux= random.nextInt(400,800);
- synchronized(lista) {
- while(lista.isEmpty()){
- System.out.println("Consumidor ("+indice+") no hay elementos");
- try {
- lista.wait();
- System.out.println("Consumidor ("+indice+") listo para consumir");
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- try {
- Thread.sleep(aux);
- consumido = lista.remove(0);
- System.out.println("Consumido "+consumido+" por consumidor ("+indice+")");
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement