< prev index next >

modules/javafx.web/src/main/java/com/sun/webkit/WatchdogTimer.java

Print this page




   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.webkit;
  27 

  28 import java.util.concurrent.ScheduledFuture;
  29 import java.util.concurrent.ScheduledThreadPoolExecutor;
  30 import java.util.concurrent.ThreadFactory;
  31 import java.util.concurrent.TimeUnit;
  32 import java.util.concurrent.atomic.AtomicInteger;
  33 import static java.util.logging.Level.WARNING;
  34 import java.util.logging.Logger;
  35 
  36 final class WatchdogTimer {
  37 
  38     private static final Logger logger =
  39             Logger.getLogger(WatchdogTimer.class.getName());
  40     private static final ThreadFactory threadFactory =
  41             new CustomThreadFactory();
  42 
  43 
  44     // The executor is intentionally made a non-static/instance
  45     // field (as opposed to a static/class field) in order for
  46     // fwkDestroy() to be able to orderly shutdown the executor
  47     // and wait for the outstanding runnable, if any, to complete.
  48     // Currently, there is just a single instance of this class
  49     // per process, so having a non-static executor should not
  50     // be a problem.
  51     private final ScheduledThreadPoolExecutor executor;
  52     private final Runnable runnable;
  53     private ScheduledFuture<?> future;
  54 
  55 
  56     private WatchdogTimer(final long nativePointer) {
  57         executor = new ScheduledThreadPoolExecutor(1, threadFactory);
  58         executor.setRemoveOnCancelPolicy(true);
  59 
  60         runnable = () -> {
  61             try {
  62                 twkFire(nativePointer);
  63             } catch (Throwable th) {
  64                 logger.log(WARNING, "Error firing watchdog timer", th);
  65             }
  66         };
  67     }
  68 
  69 
  70     private static WatchdogTimer fwkCreate(long nativePointer) {
  71         return new WatchdogTimer(nativePointer);
  72     }
  73 
  74     private void fwkStart(double limit) {
  75         if (future != null) {
  76             throw new IllegalStateException();
  77         }
  78         future = executor.schedule(runnable, (long) (limit * 1000) + 50,
  79                 TimeUnit.MILLISECONDS);
  80     }
  81 
  82     private void fwkStop() {
  83         if (future == null) {
  84             throw new IllegalStateException();




   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.webkit;
  27 
  28 import com.sun.javafx.logging.PlatformLogger;
  29 import java.util.concurrent.ScheduledFuture;
  30 import java.util.concurrent.ScheduledThreadPoolExecutor;
  31 import java.util.concurrent.ThreadFactory;
  32 import java.util.concurrent.TimeUnit;
  33 import java.util.concurrent.atomic.AtomicInteger;


  34 
  35 final class WatchdogTimer {
  36 
  37     private static final PlatformLogger logger =
  38             PlatformLogger.getLogger(WatchdogTimer.class.getName());
  39     private static final ThreadFactory threadFactory =
  40             new CustomThreadFactory();
  41 
  42 
  43     // The executor is intentionally made a non-static/instance
  44     // field (as opposed to a static/class field) in order for
  45     // fwkDestroy() to be able to orderly shutdown the executor
  46     // and wait for the outstanding runnable, if any, to complete.
  47     // Currently, there is just a single instance of this class
  48     // per process, so having a non-static executor should not
  49     // be a problem.
  50     private final ScheduledThreadPoolExecutor executor;
  51     private final Runnable runnable;
  52     private ScheduledFuture<?> future;
  53 
  54 
  55     private WatchdogTimer(final long nativePointer) {
  56         executor = new ScheduledThreadPoolExecutor(1, threadFactory);
  57         executor.setRemoveOnCancelPolicy(true);
  58 
  59         runnable = () -> {
  60             try {
  61                 twkFire(nativePointer);
  62             } catch (Throwable th) {
  63                 logger.warning("Error firing watchdog timer", th);
  64             }
  65         };
  66     }
  67 
  68 
  69     private static WatchdogTimer fwkCreate(long nativePointer) {
  70         return new WatchdogTimer(nativePointer);
  71     }
  72 
  73     private void fwkStart(double limit) {
  74         if (future != null) {
  75             throw new IllegalStateException();
  76         }
  77         future = executor.schedule(runnable, (long) (limit * 1000) + 50,
  78                 TimeUnit.MILLISECONDS);
  79     }
  80 
  81     private void fwkStop() {
  82         if (future == null) {
  83             throw new IllegalStateException();


< prev index next >