1 17 18 package org.apache.tomcat.util.net; 19 20 import java.net.Socket ; 21 22 import org.apache.tomcat.util.threads.ThreadWithAttributes; 23 24 27 class MasterSlaveWorkerThread implements Runnable { 28 29 protected PoolTcpEndpoint endpoint; 30 protected String threadName; 31 protected boolean stopped = false; 32 private Object threadSync = new Object (); 33 private Thread thread = null; 34 private boolean available = false; 35 private Socket socket = null; 36 private TcpConnection con = new TcpConnection(); 37 private Object [] threadData = null; 38 39 40 public MasterSlaveWorkerThread(PoolTcpEndpoint endpoint, String threadName) { 41 this.endpoint = endpoint; 42 this.threadName = threadName; 43 } 44 45 46 55 synchronized void assign(Socket socket) { 56 57 while (available) { 59 try { 60 wait(); 61 } catch (InterruptedException e) { 62 } 63 } 64 65 this.socket = socket; 67 available = true; 68 notifyAll(); 69 70 } 71 72 73 77 private synchronized Socket await() { 78 79 while (!available) { 81 try { 82 wait(); 83 } catch (InterruptedException e) { 84 } 85 } 86 87 Socket socket = this.socket; 89 available = false; 90 notifyAll(); 91 92 return (socket); 93 94 } 95 96 97 98 102 public void run() { 103 104 while (!stopped) { 106 107 Socket socket = await(); 109 if (socket == null) 110 continue; 111 112 endpoint.processSocket(socket, con, threadData); 114 115 endpoint.recycleWorkerThread(this); 117 118 } 119 120 synchronized (threadSync) { 122 threadSync.notifyAll(); 123 } 124 125 } 126 127 128 131 public void start() { 132 threadData = endpoint.getConnectionHandler().init(); 133 thread = new ThreadWithAttributes(null, this); 134 thread.setName(threadName); 135 thread.setDaemon(true); 136 thread.start(); 137 } 138 139 140 143 public void stop() { 144 stopped = true; 145 assign(null); 146 thread = null; 147 threadData = null; 148 } 149 150 151 } 152 | Popular Tags |