In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the method of communication between Applet". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what is the method of communication between Applet"?
Communication between Applet (2) search each other
Using static variables for communication does not mean that applet can be initialized at the same time. Different instances are started one after another, and we can't guarantee their startup order. However, one thing is certain: the class ColorRelay is 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 using static variables, as multiple instances may access static variables at the same time. To solve this problem, I used two synchronization methods (synchronized methods) to add or subtract applet instances from the linked list. Because they are synchronized static methods, when they run, the ColorRelay class is trapped to avoid concurrent access. Listing 1.3 is the code for these two methods. It is worth noting that when the first element is added to the linked list, the master thread (controller thread) starts. We will see later that the thread stops automatically 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 ())
Relayer.start ()
}
Else {
Elem.prev = listTail
ListTail.next = listTail = elem
Elem.next = null
}
}
/ * *
* Removes an instance from the 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 # addToList
, /
Static synchronized void removeFromList (ColorRelay elem) {
ColorRelay curr = list
While (curr! = null & & curr! = elem) {
Curr = curr.next
}
If (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
}
/ / If curr is null, then the element is not on the list
/ / at all. We could treat that as an error, but Isimm
/ / choosing to report success.
Return
} initialize shared data
After the Applet is created, the init method is called, which checks, converts, and stores the parameters of the applet. The image parameter requires extra attention because it is stored in another static variable. Lock the ColorRelay class before trying to access the originalImage static variable. Instead of using the synchronized method, we use a synchronized monitor statement to do this. In fact, only one ColorRelay instance should get the image parameter, but we have taken the above precautions to prevent coding errors in HTML. Listing 1. 4 is the code for 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 applet, the start method is called to add applet to the linked list. In the stop method, applet is translated out of the linked list. As you have seen in the previous code, the addition of the first linked list element will cause the control thread to start. This control thread simply loops through the linked list elements, lighting up one element in the linked list at a time (showing a color picture). As for the duration of the display, it is up to applet to decide. If there are no elements in the linked list, the control thread terminates automatically. Listing 1. 5 is the strat, stop, and run methods that control 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 ()
}
Catch (InterruptedException e) {
}
}
}
} at this point, I believe you have a deeper understanding of "what is the method of communication between Applet". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.