Applet間的通訊(2)--Tricks of the Java Programming Gurus (轉)

worldblog發表於2007-12-09
Applet間的通訊(2)--Tricks of the Java Programming Gurus (轉)[@more@]

Tricks
 of the
  Programming Gurus

by Glenn L. Vanderburg. et al.

1.Applet間的通訊(2)

相互查詢

  使用靜態變數進行通訊並不意味著applet就可以同時被初始化。不同的例項還是先後啟動的,我們並不能保證它們的啟動次序。然而,有一點我們可以確定:在第一個ColorRelay applet例項被建立之前,類ColorRelay已經初始化了,因此,當所有applet例項啟動時,它們都將獲得類的靜態變數。

  但是,當你使用靜態變數時必須小心,因為多個例項可能同時訪問靜態變數。為了解決這個問題,我使用了兩個同步方法(synchronized methods)從連結串列中增減applet例項。因為它們是synchronized static方法,當它們執行時,ColorRelay 類會被索住,以避免併發訪問。清單1.3是這兩個方法的程式碼。值得注意的是,當第一個元素被加到連結串列之後,主控執行緒(controller thread)將啟動。我們隨後會看到當最後一個元素從連結串列中被移出後,這個執行緒將自動停止。


清單 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 proy 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 I'm
 // choosing to report success.

 return;
 }

初始化共享資料

  Applet被建立之後init方法被,這個方法檢查、轉換和applet的引數。對image引數需要額外的注意,因為它是儲存在另一個靜態變數中的。在試圖訪問originalImage靜態變數之前,要鎖住ColorRelay類,我們沒有使用synchronized 方法,而是使用一段synchronized 監測語句來實現這個目的。(事實上,應該只有一個ColorRelay例項獲得image引數,但為了防止HTML中的編碼錯誤,我們採取了上述的預防措施)。清單1.4是init的程式碼。


清單 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.Saet.tjg.ColorRelay");
 synchronized (cr) {
 if (originalImage == null) {
 originalImage = getImage(getDocumentBase(), imageURL);
 }
 }
 }

 tracker = new MediaTracker(this);
 }

Working Together

  當準備applet時,start 方法被呼叫,將applet加入連結串列。在stop方法中,applet被譯出連結串列。在前面的程式碼中你已經看到,第一個連結串列元素的新增將導致控制執行緒的啟動。這個控制執行緒僅僅是迴圈讀取連結串列元素,一次將連結串列中的某個元素點亮(顯示彩色圖片)。至於顯示的持續時間,是由applet自己決定的。如果連結串列中沒有元素了,控制執行緒自動終止。清單1.5是控制執行緒的strat、stop以及run方法。


清單 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 cerating 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) {
 }
 }
 } 
 }

翻譯:chenyuan_tongji (to:chenyuan_tongji@sina.com">chenyuan_tongji@sina.com)

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-990231/,如需轉載,請註明出處,否則將追究法律責任。

相關文章