{ while(thue) { gate.pass(myname, myaddress); } } } public class Main { public static void main(String[] args) { Gate gate = new Gate(); new UserThread(gate, “Alice”, “Alaska”).start(); new UserThread(gate, “Bobby”, “Brazil”).start(); new UserThread(gate, “Chris”, “Canada”).start(); } }
private int waitingWriters = 0; // 한번에 한 쓰레드만 쓰기가 가능하기 때문에 대기자를 기록. private int writingWriters = 0; // 읽기, 쓰기 한쪽이 굶어 죽는 것을 방지. private boolean preferWriter = true; public synchronized void readLock() { // 쓰기 락이 걸렸거나 기다리는 쓰기 락을 걸어줘야 할 때. while (writingWriters > 0 || (preferWriter && waitingWriters > 0)) { wait(); } readingReaders++; } public synchronized void readUnlock() { readingReaders--; preferWriter = true; notifyAll(); }
final char c) { new Thread() { public void run() { helper.handle(count, c); } }.start(); } } public class Main { public static void Main(String[] args) { Host host = new Host(); host.request(10, ‘A’); host.request(20, ‘B’); host.request(30, ‘C’); } }
char c) { final FutureData future = new FutureData(); new Thread() { public void run() { RealData realData = new RealData(count, c); future.setRealData(realData); } }.start(); return future; } }