home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1997 July / PCO_07_97.ISO / NET_COM / cc32e40.exe / nav40.z / java40.jar / netscape / applet / AppletThreadKiller.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-06-07  |  4.2 KB  |  203 lines

  1. package netscape.applet;
  2.  
  3. class AppletThreadKiller implements Runnable {
  4.    AppletThreadList list = new AppletThreadList();
  5.    Thread[] threadsSnapshot;
  6.    int threadsSnapshotLen;
  7.    boolean keepRunning = true;
  8.    static ThreadGroup clientThreadGroup;
  9.    static final int SHUTDOWN_TIMEOUT = 30000;
  10.  
  11.    AppletThreadKiller() {
  12.       SecurityManager.enablePrivilege("UniversalThreadGroupAccess");
  13.       clientThreadGroup = new ThreadGroup("ClientThreadGroup");
  14.       Thread var1 = new Thread(this, "Applet Thread Killer");
  15.       var1.start();
  16.    }
  17.  
  18.    synchronized void addAppletThread(AppletThreadList var1) {
  19.       this.list.appendElement(var1);
  20.       this.notify();
  21.    }
  22.  
  23.    synchronized AppletThreadList getAppletThread() {
  24.       AppletThreadList var1 = null;
  25.  
  26.       while(this.keepRunning && this.list.isEmptyList()) {
  27.          try {
  28.             this.wait();
  29.          } catch (InterruptedException var2) {
  30.          }
  31.       }
  32.  
  33.       if (!this.list.isEmptyList()) {
  34.          var1 = (AppletThreadList)this.list.next;
  35.          ((DoubleLinkedList)var1).remove();
  36.       }
  37.  
  38.       return var1;
  39.    }
  40.  
  41.    synchronized boolean shutdown() {
  42.       if (MozillaAppletContext.debug >= 7) {
  43.          System.out.println("##### shutting down all applets");
  44.       }
  45.  
  46.       this.keepRunning = false;
  47.       this.notify();
  48.  
  49.       try {
  50.          this.wait((long)(30000 * (MozillaAppletContext.totalApplets + 1)));
  51.       } catch (InterruptedException var2) {
  52.          if (MozillaAppletContext.debug >= 7) {
  53.             System.out.println("##### applets shutdown interrupted: " + var2);
  54.          }
  55.       }
  56.  
  57.       boolean var1 = clientThreadGroup.activeCount() == 0;
  58.       if (MozillaAppletContext.debug >= 7) {
  59.          System.out.println("##### applets shutdown complete: " + (var1 ? "successful" : "failed"));
  60.       }
  61.  
  62.       return var1;
  63.    }
  64.  
  65.    public void run() {
  66.       while(this.keepRunning || !this.list.isEmptyList()) {
  67.          try {
  68.             AppletThreadList var1 = this.getAppletThread();
  69.             if (var1 != null) {
  70.                Object var2 = null;
  71.                Object var3 = null;
  72.                MozillaAppletContext var4 = null;
  73.                Object var5 = null;
  74.                ThreadGroup var82 = var1.group;
  75.                DerivedAppletFrame var83 = var1.frame;
  76.                if (var83 != null) {
  77.                   var4 = var83.context;
  78.                }
  79.  
  80.                Task var84 = var1.task;
  81.                String var6 = var83 != null ? var83.getAppletName() : var84.taskName();
  82.                synchronized(var82){}
  83.  
  84.                try {
  85.                   if (var83 != null) {
  86.                      var83.requestShutdown();
  87.                   } else {
  88.                      var84.requestShutdown();
  89.                   }
  90.  
  91.                   if (!this.keepRunning) {
  92.                      int var9 = var82.activeCount();
  93.                      if (this.threadsSnapshot == null || this.threadsSnapshotLen < var9) {
  94.                         this.threadsSnapshot = new Thread[var9];
  95.                         this.threadsSnapshotLen = var9;
  96.                      }
  97.  
  98.                      var82.enumerate(this.threadsSnapshot);
  99.  
  100.                      for(int var10 = 0; var10 < var9; ++var10) {
  101.                         Thread var11 = this.threadsSnapshot[var10];
  102.                         if (var11.isDaemon()) {
  103.                            var11.stop();
  104.                         }
  105.                      }
  106.                   }
  107.  
  108.                   if (var82.activeCount() > 0) {
  109.                      try {
  110.                         var82.wait(30000L);
  111.                      } catch (InterruptedException var76) {
  112.                         System.err.println("# Interrupted while waiting for applet to die: " + var6);
  113.                      }
  114.                   }
  115.  
  116.                   if (var82.activeCount() > 0) {
  117.                      if (MozillaAppletContext.debug >= 4) {
  118.                         System.out.println("# Applet " + var6 + " did not shut down within " + 30 + " seconds -- killing it.");
  119.                      }
  120.  
  121.                      try {
  122.                         SecurityManager.enablePrivilege("UniversalThreadGroupAccess");
  123.                         var82.stop();
  124.                         var82.wait(5000L);
  125.                      } catch (InterruptedException var74) {
  126.                         System.err.println("# Interrupted while stopping thread group: " + var6);
  127.                      } finally {
  128.                         SecurityManager.revertPrivilege();
  129.                      }
  130.                   }
  131.  
  132.                   if (var82.activeCount() > 0) {
  133.                      try {
  134.                         SecurityManager.enablePrivilege("UniversalThreadGroupAccess");
  135.                         var82.destroy();
  136.                      } catch (Exception var78) {
  137.                         if (MozillaAppletContext.debug >= 7) {
  138.                            System.err.println("# Exception while destroying group: " + var6);
  139.                            ((Throwable)var78).printStackTrace();
  140.                         }
  141.                      } finally {
  142.                         SecurityManager.revertPrivilege();
  143.                      }
  144.                   }
  145.  
  146.                   if (var83 != null) {
  147.                      var83.destroy();
  148.                   }
  149.                } catch (Throwable var80) {
  150.                   throw var80;
  151.                }
  152.  
  153.                if (var4 != null) {
  154.                   synchronized(var4){}
  155.  
  156.                   try {
  157.                      if (var4.appletFrames != null && var4.appletFrames.isEmpty()) {
  158.                         if (MozillaAppletContext.debug >= 7) {
  159.                            System.err.println("#   destroyApplet: destroying context for contextID " + var4.contextID);
  160.                         }
  161.  
  162.                         var4.destroy();
  163.                      }
  164.                   } catch (Throwable var77) {
  165.                      throw var77;
  166.                   }
  167.                }
  168.             }
  169.          } catch (Exception var81) {
  170.             System.out.println("Exception occurred while destroying applet: " + ((Throwable)var81).toString());
  171.             if (MozillaAppletContext.debug >= 7) {
  172.                ((Throwable)var81).printStackTrace();
  173.             }
  174.          }
  175.       }
  176.  
  177.       try {
  178.          SecurityManager.enablePrivilege("UniversalThreadGroupAccess");
  179.          clientThreadGroup.stop();
  180.          clientThreadGroup.destroy();
  181.       } finally {
  182.          SecurityManager.revertPrivilege();
  183.       }
  184.  
  185.       if (MozillaAppletContext.debug >= 7) {
  186.          System.out.println("# all applets and programs killed");
  187.       }
  188.  
  189.       synchronized(this){}
  190.  
  191.       try {
  192.          this.notify();
  193.       } catch (Throwable var72) {
  194.          throw var72;
  195.       }
  196.  
  197.    }
  198.  
  199.    public String toString() {
  200.       return new String("Applet thread killer! Applets to be disposed: " + this.list);
  201.    }
  202. }
  203.