1 2 package cpmake; 3 4 import java.util.*; 5 import java.io.*; 6 7 8 9 class InputWatcher 10 implements Runnable 11 { 12 private static InputWatcher s_inputWatcher = null; 13 14 private Set m_runningProcesses; 15 16 private InputWatcher() 17 { 18 m_runningProcesses = new HashSet(); 19 Thread t = new Thread (this); 20 t.start(); 21 } 22 23 public static InputWatcher getInputWatcher() 25 { 26 if (s_inputWatcher == null) 27 s_inputWatcher = new InputWatcher(); 28 29 return (s_inputWatcher); 30 } 31 32 public void addProcess(Process proc) 34 { 35 36 synchronized(m_runningProcesses) 37 { 38 m_runningProcesses.add(proc); 39 } 40 } 41 42 public void removeProcess(Process proc) 44 { 45 synchronized(m_runningProcesses) 46 { 47 m_runningProcesses.remove(proc); 48 } 49 } 50 51 private Process getProcess() 53 { 54 Process proc = null; 55 Iterator it; 56 57 synchronized(m_runningProcesses) 58 { 59 it = m_runningProcesses.iterator(); 60 if (it.hasNext()) 61 proc = (Process )it.next(); 62 } 63 64 return (proc); 65 } 66 67 public void run() 69 { 70 InputStreamReader input = new InputStreamReader(System.in); 71 int ch = 0; 72 OutputStreamWriter output; 73 Process proc; 74 75 try 76 { 77 while ((ch = input.read()) != -1) 78 { 79 Thread.yield(); 80 proc = getProcess(); 81 if (proc != null) 82 { 83 output = new OutputStreamWriter(proc.getOutputStream()); 84 output.write(ch); 85 output.flush(); 86 } 87 } 88 } 89 catch(IOException ioe) 90 { 91 } 92 } 93 } 94 | Popular Tags |