Developing Java Beans { lowThreshold = low; }
Friday, September 29th, 2006Developing Java Beans We could assume that if an event generator didn’t want to be interrupted for long periods of time, it would be implemented to protect itself from this possibility. But another approach is to use an event adapter that queues events and forwards them to the target object on another thread. This type of asynchronous delivery allows the thread that originally fired the event to continue doing whatever it wants, because the method call it made when firing the event returns almost immediately. Let’s create an example to illustrate how to build this type of adapter. We have a worker object that runs a thread every 200 milliseconds in order to update a counter. This object will be called a Poller. On every third update it generates a PollEvent to a listening object that implements the PollerListener interface, and it also prints the string “Value: x,” where x represents the value of the counter. Here’s the code. import java.util.*; class PollEvent extends EventObject { // the event valueprotected int value; // constructor takes source and value PollEvent(Object source, int value) { // pass the source to the superclass super(source); // save the value this.value = value; } // return the event value public int getValue() { return value; } } interface PollerListener { public abstract void pollSent(PollEvent e); } class Poller extends Thread { // a counter protected int cnt = 0; // the listener protected PollerListener listener = null; // the constructor public Poller() { // start my thread start(); } // add the listener public void addPollerListener(PollerListener l) throws TooManyListenersException { if (listener != null) { page 45
Note: If you are looking for cheap and reliable provider to host and run your servlet application check Vision servlet hosting services