Untitled

                Never    
Java
       
import java.util.ArrayList;
import java.util.Queue;

//1 Implemente, em Java, um vetor que seja seguro para uso com threads. Os metodos get set e swap devem ser implementados
/*
public class p2 {
	private int[] list;
	private int tam;
	
	public p2(int tam) {
		this.tam = tam;
		this.list = new int[tam];
	}
	private synchronized int get(int index) {
		return this.list[index];
	}
	private synchronized void set(int index, int value) {
		this.list[index] = value;
	}	
	private synchronized void swap(int index1, int index2) {
		int aux = this.list[index1];
		this.list[index1] = this.list[index2];
		this.list[index2] = aux;
	}
}
*/
//2 Implemente uma fila bloqueante em Java. Nao usar funcoes da biblioteca de Java.
/*
public class p2 {
	//blockingqueue class
	private ArrayList<Integer> oi;
	private Semaphore semaphore;
	
	public p2(ArrayList<Integer> oi, Semaphore sema) {
		this.oi = oi;
		this.semaphore = sema;
	}	
	private void add(int eae) throws InterruptedException {
		this.semaphore.take();
		oi.add(eae);
		this.semaphore.put();
	}	
	private void remove(int index) throws InterruptedException {
		this.semaphore.take();
		oi.remove(index);
		this.semaphore.put();
	}
}

class Semaphore {
	private boolean lock;
	
	public Semaphore() {
		this.lock = false;
	}
	public synchronized void put() {
		this.lock = false;
		notify();
	}	
	public synchronized void take() throws InterruptedException {
		if(this.lock) {
			wait();
			take();
		} else {
			this.lock = true;
		}		
	}	 	 	
}
*/
//3a questão
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * Created by sttf on 26/06/18.
 */
public class p2 {
	private BlockingQueue<String> forno;

	public p2() {
		this.forno = new ArrayBlockingQueue<>(50);
	}

	public void assar() {
	}

	public static void main(String[] args) throws InterruptedException {
		p2 p = new p2();
		(new Thread(new Abastecer(p.forno))).start();
	}

	public static class Abastecer extends Thread {
//Caso o forno esteja vazio, abastecemos 50 pães, 10 por vez a cada 0.5s
		private BlockingQueue<String> forno;

		public Abastecer(BlockingQueue bq) {
			this.forno = bq;
		}

		@Override
		public void run() {
			while (true) {
				if (this.forno.size() == 0) {
					for (int j = 0; j < 5; j++) {
						for (int i = 0; i < 10; i++) {
							this.forno.add("Pão");
						}
						System.out.println("10 pães colocados no forno");
						(new Thread(new Assar(this.forno))).start();
						try {
							Thread.sleep(500);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
					System.out.println("forno abastecido");
				}
			}
		}
	}

	public static class Assar extends Thread {
		private BlockingQueue<String> forno;

		public Assar(BlockingQueue bq) {
			this.forno = bq;
		}

		@Override
		public void run() {
//A cada 1.5s pães ficam prontos e são retirados
			if (this.forno.size() > 0) {
//Retiro 10 por vez já que são colocados ao mesmo tempo
				for (int i = 0; i < 10; i++) {
					this.forno.remove();
				}
				System.out.println("1 lote(10) de pão assado removido");
			}
		}
	}
}

Raw Text