ICE 3.4.2
[php5-ice-freebsdport.git] / java / test / Ice / dispatcher / Dispatcher.java
blobce8a261ef0b787693a6bd06975ec37a12f88db9a
1 // **********************************************************************
2 //
3 // Copyright (c) 2003-2011 ZeroC, Inc. All rights reserved.
4 //
5 // This copy of Ice is licensed to you under the terms described in the
6 // ICE_LICENSE file included in this distribution.
7 //
8 // **********************************************************************
10 package test.Ice.dispatcher;
12 public class Dispatcher implements Runnable, Ice.Dispatcher
14 private static void
15 test(boolean b)
17 if(!b)
19 throw new RuntimeException();
23 public Dispatcher()
25 _thread = new Thread(this);
26 _thread.start();
29 public void
30 run()
32 while(true)
34 Runnable call = null;
35 synchronized(this)
37 if(!_terminated && _calls.isEmpty())
39 try
41 wait();
43 catch(java.lang.InterruptedException ex)
48 if(!_calls.isEmpty())
50 call = _calls.poll();
52 else if(_terminated)
54 // Terminate only once all calls are dispatched.
55 return;
59 if(call != null)
61 try
63 call.run();
65 catch(Exception ex)
67 // Exceptions should never propagate here.
68 test(false);
74 synchronized public void
75 dispatch(Runnable call, Ice.Connection con)
77 boolean added = _calls.offer(call);
78 assert(added);
79 if(_calls.size() == 1)
81 notify();
85 public void
86 terminate()
88 synchronized(this)
90 _terminated = true;
91 notify();
93 while(true)
95 try
97 _thread.join();
98 break;
100 catch(java.lang.InterruptedException ex)
106 public boolean
107 isDispatcherThread()
109 return Thread.currentThread() == _thread;
112 private java.util.Queue<Runnable> _calls = new java.util.LinkedList<Runnable>();
113 private Thread _thread;
114 private boolean _terminated = false;