Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Producer implements Runnable {
- private final List < Integer > bufferCondiviso;
- //…
- public Producer(List < Integer > bufferCondiviso, int size) {
- this.bufferCondiviso = bufferCondiviso;
- }
- @Override public void run() {
- while (true) {
- try {
- produce(); /* Il thread Producer prova a riempire il buffer... */
- } catch (InterruptedException ex) {
- ex.printStackTrace();
- }
- }
- }
- private void produce(int i) throws InterruptedException {
- /* il thread resta in stato wait se il buffer è pieno */
- while (bufferCondiviso.size() == SIZE) {
- synchronized(bufferCondiviso) {
- //…
- bufferCondiviso.wait();
- }
- }
- /* il buffer non è pieno, quindi il thread può aggiungere un nuovo elemento e notificarlo al consumer */
- synchronized(bufferCondiviso) {
- bufferCondiviso.add(i);
- bufferCondiviso.notifyAll();
- }
- }
- }
Add Comment
Please, Sign In to add comment