?? auton.html
字號:
<html><!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"><html> <head><title>Autonomous Loops</title></head><BODY bgcolor=#ffffee vlink=#0000aa link=#cc0000><h1>Autonomous Loops</h1>Some activities intrinsically take the form of ``continuous loops'',that forever execute some kind of state update loop in their<code>run</code> method. Common examples include ``autonomous activeobjects'' -- intrinsically active objects that continually changestate independently of any other object, for example some forms ofsimulated animated objects used in Applets. (See also the <ahref="acceptor.html" tppabs="http://www.foi.hr/~dpavlin/java/mirrors/g.oswego.edu/dl/pats/acceptor.html">Event Loop</a> pattern.)<p> The strategy of choice here is also the simplest: Establish a newthread whenever you create an object of this form. To fully automateautonmous behavior, it's possible to have <code>(newThread(this)).start()</code> right in the constructor for each suchobject. But this has the disadvantage of precluding use by other objectsthat want to construct them in one phase, and start them up later.Also, since they never terminate, autonomous objects are normallyfired up as <a href="javaconc.html" tppabs="http://www.foi.hr/~dpavlin/java/mirrors/g.oswego.edu/dl/pats/javaconc.html"><em>daemon</em> threads</a>and run at relatively low priorities, both of which should normallybe controlled by the client starting up the thread.<p> To set up an example, we can put together a mini-framework forautonomous objects that are drawable in Applets. This is slightlytricky to pull off using the Java AWT. One way to do it is to provide a``loopback'' so that when an autonomous object changes in a way thatrequires a screen update, it asks its controlling Applet to do a<code>repaint</code>, which in turn requests that the autonomousobject <code>paint</code> itself. (One reason for using this loopbackdesign is that the Applet receives the <code>Graphics</code> from thescreen updater that is needed by <code>paint</code>. There are otherways to accomodate this as well; for example initially handing theobject a Canvas to use for all subsequent drawing.)<p>To scaffold this, we can define the following abstractions:<pre>public interface AutonomousGraphic extends Runnable { public void paint(Graphics g);}public abstract class AutonomousGraphicApplet extends Applet { protected abstract AutonomousGraphic model(); public void start() { (new Thread(model())).start(); } public void paint(Graphics g) { model().paint(g); }}</pre><p>These allow creation of different kinds of <code>AutonomousGraphic</code>implementations, controlled by different subclasses of<code>AutonomousGraphicApplet</code>. For example, we could instantiatethe autonomous graphics as:<pre>public class ChangingText implements AutonomousGraphic { private String msg_; private Rectangle drawingArea_; private Color color_; private Applet app_; public ChangingText(Applet app, Rectangle area, String msg) { app_ = app; msg_ = msg; drawingArea_ = area; color_ = Color.black; } public void run() { for (;;) { try { Thread.currentThread().sleep(1000); } catch(InterruptedException ex) { return; } color_ = new Color((int)(Math.random() * 255.0), (int)(Math.random() * 255.0), (int)(Math.random() * 255.0)); app_.repaint(); } } public void paint(Graphics g) { g.setColor(color_); // should actually clip to fit in this rectangle g.drawString(msg_, drawingArea_.x, drawingArea_.y); }}</pre><p>This can be controlled by:<pre>public class ChangingTextApplet extends AutonomousGraphicApplet { protected AutonomousGraphic model_; protected AutonomousGraphic model() { return model_; } public void init() { model_ = new ChangingText(this, new Rectangle(10, 10, 200, 40), "ABCDEFGHIJKLMNOPQRSTUVWXYZ"); }}</pre><p><a href="ChangingTextApplet.html" tppabs="http://www.foi.hr/~dpavlin/java/mirrors/g.oswego.edu/dl/pats/ChangingTextApplet.html">Run ChangingtextApplet</a>.<h3><a name="secYielding"></a>Scheduling and Yielding</h3><p> To avoid cases where a single thread ``takes over'' a CPU, classeswith autonomous loops should be written in a way that guarantees thatthey occasionally yield control back to the <ahref="javaconc.html" tppabs="http://www.foi.hr/~dpavlin/java/mirrors/g.oswego.edu/dl/pats/javaconc.html">scheduler</a>. Threads automatically yield inmany situations, for example when they are waiting for input, so youdon't often need to worry about yielding control of a thread to ensurethat other tasks get a chance to run. The main exception is forcontinuous loop <code>run()</code> methods that do not otherwise makeany method calls that would cause them to block. For example, in somesimulations of autonomous objects, you might have code of the form:<pre>class RandomlyMovingPoint implements Runnable { double xcoord_; double ycoord_; ... public void run() { for (;;) { xcoord_ += Math.random() - 0.5; ycoord_ += Math.random() - 0.5; Thread.currentThread().yield(); } }}</pre><p>In most contexts, it would make more sense to use<code>sleep</code> to control simulation rates, and/or to interactwith other objects upon state changes. In the first (and normallythe second) cases, this would automatically take care of yielding,but it is a useful safeguard to explicitly <code>yield</code> anyway.<p> An alternative, more fine-grained means of control is to set up a <ahref="service.html#secCallbacks" tppabs="http://www.foi.hr/~dpavlin/java/mirrors/g.oswego.edu/dl/pats/service.html#secCallbacks">callback</a> to the client (or anyother manager object) after each iteration of the loop, and thenwaiting (via standard <a href="synchDesign.html" tppabs="http://www.foi.hr/~dpavlin/java/mirrors/g.oswego.edu/dl/pats/synchDesign.html">synchronization</a>mechanics or via <a href="javaconc.html" tppabs="http://www.foi.hr/~dpavlin/java/mirrors/g.oswego.edu/dl/pats/javaconc.html"><code>suspend/resume</code></a>) until it signals resumption. (Suchcallbacks can also help serve as ``keepalives'' and progressindicators.)<p><a href="aopintro.html" tppabs="http://www.foi.hr/~dpavlin/java/mirrors/g.oswego.edu/dl/pats/aopintro.html">[Concurrent Programming in Java]</a><hr><address><A HREF="javascript:if(confirm('http://g.oswego.edu/dl \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://g.oswego.edu/dl'" tppabs="http://g.oswego.edu/dl">Doug Lea</A></address><!-- hhmts start -->Last modified: Tue Feb 20 06:27:19 EST 1996<!-- hhmts end --></body> </html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -