1 21 22 27 28 package javax.mail; 29 30 import java.io.*; 31 import java.util.Vector ; 32 import javax.mail.event.MailEvent ; 33 34 43 class EventQueue implements Runnable { 44 45 class QueueElement { 46 QueueElement next = null; 47 QueueElement prev = null; 48 MailEvent event = null; 49 Vector vector = null; 50 51 QueueElement(MailEvent event, Vector vector) { 52 this.event = event; 53 this.vector = vector; 54 } 55 } 56 57 private QueueElement head = null; 58 private QueueElement tail = null; 59 private Thread qThread; 60 61 public EventQueue() { 62 qThread = new Thread (this, "JavaMail-EventQueue"); 63 qThread.setDaemon(true); qThread.start(); 65 } 66 67 70 public synchronized void enqueue(MailEvent event, Vector vector) { 71 QueueElement newElt = new QueueElement(event, vector); 72 73 if (head == null) { 74 head = newElt; 75 tail = newElt; 76 } else { 77 newElt.next = head; 78 head.prev = newElt; 79 head = newElt; 80 } 81 notify(); 82 } 83 84 92 private synchronized QueueElement dequeue() 93 throws InterruptedException { 94 while (tail == null) 95 wait(); 96 QueueElement elt = tail; 97 tail = elt.prev; 98 if (tail == null) { 99 head = null; 100 } else { 101 tail.next = null; 102 } 103 elt.prev = elt.next = null; 104 return elt; 105 } 106 107 110 public void run() { 111 QueueElement qe; 112 113 try { 114 loop: 115 while ((qe = dequeue()) != null) { 116 MailEvent e = qe.event; 117 Vector v = qe.vector; 118 119 for (int i = 0; i < v.size(); i++) 120 try { 121 e.dispatch(v.elementAt(i)); 122 } catch (Throwable t) { 123 if (t instanceof InterruptedException ) 124 break loop; 125 } 127 128 qe = null; e = null; v = null; 129 } 130 } catch (InterruptedException e) { 131 } 133 } 134 135 138 void stop() { 139 if (qThread != null) { 140 qThread.interrupt(); qThread = null; 142 } 143 } 144 } 145 | Popular Tags |