Communication between applets (2) - Tricks of the Java Programming Gurus

zhaozj2021-02-17  56

Tricks of the java programing gurus

By Glenn L. Vanderburg. et al.

1.Applets Communication (2)

Find each other

Communication with static variables does not mean that applets can be initialized simultaneously. Different instances are still started, we do not guarantee their startup order. However, there is a little we can determine: The class colorrelay has been initialized before the first ColorRelay Applet instance is created, so when all Applet instances are started, they will get the static variables of the class.

However, you must be careful when you use static variables, because multiple instances may access static variables simultaneously. In order to solve this problem, I use two synchronized methods to add subtracks instances from the list. Because they are synchronized static methods, the ColorRelay class will be saved when they run to avoid concurrent access. Listing 1.3 is the code for both methods. It is worth noting that after the first element is applied to the list, the control thread will start. We will then see that this thread will automatically stop when the last element is removed from the linked list.

Listing 1.3. ColorRelay.java (Part 2).

/ ** * Adds an instance to the list of active instances maintained in the * class. No check is made to prevent adding the same instance twice. * @Param elem the ColorRelay instance to add to the list. * @See #removeFromList * / static synchronized void addToList (ColorRelay elem) {if (list == null) {list = listTail = elem; elem.next = elem.prev = null; // Because the list has elements now, we should start the thread relayer. = New THREAD (NEW colorRelay ();} else {elem.prev = listtail; listtail.next = listtail = elem; elem.next = null;}} / ** * Removes an instance from there list of active instances maintained in * the class. Works properly but does not signal an error if * the element was not actually on the list. * @param elem the ColorRelay instance to be removed from the list. * @see #addtolis T * / static synchronized voidfromlist (colorrelay elem) {colorrelay curr = list; while (curr! = null && curr! = elem) {curr = curr.next;}} (curr == elem) {// We found it ! If (list == curr) {list = curr.next;} if (listtail == curr) {listtail = curr.prev;} if (curr.next! = Null) {curr.next.prev = curr.prev } If (curr.prev! = Null) {curr.prev.next = curr.next;} curr.next =

Curr.PREV = NULL;} //iff curr is null, the element is not on the list // at all. we could Treat That as an error, but i'm // choosing to report success. return;} initialization Share data

After the applet is created, the init method is called, this method checks, converts, and stores the parameters of the applet. Additional attention is required for Image parameters because it is stored in another static variable. Before trying to access the OriginalImage static variable, lock the ColorRelay class, we don't use the synchronized method, but use a SYNCHRONIZED monitoring statement to achieve this. (In fact, there should be only a colorrelay instance to obtain image parameters, but in order to prevent encoding errors in HTML, we have taken the above precautions). Listing 1.4 is the code of the init.

Listing 1.4. ColorRelay.java (Part 3).

.. / ** * Initializes the applet instance Checks and stores * parameters and initializes other instance variables * / public void init () {String flash = getParameter ( "flashColor"); if (! Flash = null) {try {flashColor = NEW Color (Parsergb (Flash));} catch (NumberFormatexception E) {// ignore a bad parameter and just go with the default.}} String Sleep = getParameter ("SleepTime"); if (Sleep! = null) {TRY {sleepSecs = Integer.parseInt (sleep);} catch (NumberFormatException e) {. // Ignore a bad parameter and just go with the default}} String imageURL = getParameter ( "image"); if (! imageURL = null) { Class cr = Class.Forname ("com.mcp.samsnet.tjg.colorrelay"); synchronized (cr) {if (OriginalImage == null) { OriginalImage = GetImage (GetDocumentBase (), ImageURL);}}} Tracker = new Mediatracker (this);} Working Together

When the browser is ready to execute the applet, the Start method is called to add the applet to the list. In the STOP method, the applet is translated. In the previous code you have seen, the addition of the first linked table element will result in the launch of the control thread. This control thread is merely a loop read chain table element, and a certain element in the list is lit once (display color pictures). As for the duration of the display, it is determined by the applet itself. If there is no element in the linked list, the control thread is automatically terminated. Listing 1.5 is the STRAT, STOP, and RUN methods for controlling threads.

Listing 1.5. ColorRelay.java (Part 4).

/ ** * Starts the applet running. The ColorRelay hooks up with * other instances on the same page and begins coordinating * when this method is called. * / Public void start () {// Ordinarily, we want to display the original image . image = originalImage; ColorRelay.addToList (this);!.. // Let's get to work} / ** * Stops the applet The ColorRelay instance removes itself from the * group of cooperating applets when this method is called * / public void stop () {ColorRelay.removeFromList (this);}. / ** * Loops through the list of active instances for as long as it is * non-empty, calling each instance's 'flash' method * @see #flash * / public Void Run () {colorRelay Curr; // Continue Running Through The list Until It's Empty ... While (list! = null) {for (curr = list; curr! = null; curr = curr.next) {Try {CURR .flash (); (InterruptedException E) {}}}}

chenyuan_tongji@sina.com)

转载请注明原文地址:https://www.9cbs.com/read-30082.html

New Post(0)