Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 4th, 2012  |  syntax: None  |  size: 1.68 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /**
  2.  * @author Feder1co 5oave
  3.  */
  4. class Mailbox {
  5.        
  6.         class Message {
  7.                 Message next;
  8.                 String text;
  9.                
  10.                 Message(String t) {
  11.                         text = t;
  12.                         // next = null
  13.                 }
  14.         }
  15.        
  16.         private static final int N = 4;
  17.        
  18.         private Message[] heads, tails;
  19.         private Object[] monitors;
  20.        
  21.         public Mailbox() {
  22.                 heads = new Message[N];
  23.                 tails = new Message[N];
  24.                 monitors = new Object[N];
  25.                 for (int i = 0; i < N; i++) monitors[i] = new Object();
  26.         }
  27.        
  28.         public void invia(int tipo, String message) {
  29.                 synchronized(monitors[tipo]) {
  30.                         Message m = new Message(message);
  31.                        
  32.                         if (heads[tipo] == null) {
  33.                                 heads[tipo] = tails[tipo] = m;
  34.                         } else {
  35.                                 tails[tipo].next = m;
  36.                                 tails[tipo] = m;
  37.                         }
  38.                        
  39.                         monitors[tipo].notify();  // uno entra, uno esce
  40.                 }
  41.         }
  42.        
  43.         public String ricevi(int tipo) throws InterruptedException {
  44.                 synchronized (monitors[tipo]) {
  45.                         while (heads[tipo] == null) monitors[tipo].wait();
  46.                        
  47.                         Message ret = heads[tipo];
  48.                         if (ret.next == null) tails[tipo] = null;
  49.                         heads[tipo] = ret.next;
  50.                        
  51.                         return ret.text;
  52.                 }
  53.         }
  54. }
  55.  
  56. class Lettore extends Thread {
  57.         private Mailbox m;
  58.         private int t;
  59.        
  60.         public Lettore(int tipo, Mailbox mailbox) {
  61.                 m = mailbox;
  62.                 t = tipo;
  63.                 start();
  64.         }
  65.        
  66.         public void run() {
  67.                 String msg = "";
  68.                 do {
  69.                         try {
  70.                                 msg = m.ricevi(t);
  71.                                 System.out.println("Tipo " + t + ": " + msg);
  72.                         } catch (InterruptedException ex) {}
  73.                 } while (!msg.equals("Stop"));
  74.         }
  75. }
  76.  
  77. class Scrittore extends Thread {
  78.         private Mailbox m;
  79.         private int t;
  80.        
  81.         public Scrittore(int tipo, Mailbox mailbox) {
  82.                 m = mailbox;
  83.                 t = tipo;
  84.                 start();
  85.         }
  86.        
  87.         public void run() {
  88.                 int i = 0;
  89.                 m.invia(t, "Messaggio " + (i++));
  90.                 m.invia(t, "Messaggio " + (i++));
  91.                 m.invia(t, "Messaggio " + (i++));
  92.                 m.invia(t, "Stop");
  93.         }
  94. }