Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  Licensed to the Apache Software Foundation (ASF) under one or more
   *  contributor license agreements.  See the NOTICE file distributed with
   *  this work for additional information regarding copyright ownership.
   *  The ASF licenses this file to You under the Apache License, Version 2.0
   *  (the "License"); you may not use this file except in compliance with
   *  the License.  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
  *
  *  Unless required by applicable law or agreed to in writing, software
  *  distributed under the License is distributed on an "AS IS" BASIS,
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *  See the License for the specific language governing permissions and
  *  limitations under the License.
  */
 package org.apache.tomcat.websocket;
 
 import java.util.Set;
 
Provides a background processing mechanism that triggers roughly once a second. The class maintains a thread that only runs when there is at least one instance of BackgroundProcess registered.
 
 public class BackgroundProcessManager {
 
     private static final BackgroundProcessManager instance;
 
 
     static {
          = new BackgroundProcessManager();
     }
 
 
     public static BackgroundProcessManager getInstance() {
         return ;
     }
 
     private final Set<BackgroundProcessprocesses = new HashSet<BackgroundProcess>();
     private final Object processesLock = new Object();
     private WsBackgroundThread wsBackgroundThread = null;
 
     private BackgroundProcessManager() {
         // Hide default constructor
     }
 
 
     public void register(BackgroundProcess process) {
         synchronized () {
             if (.size() == 0) {
                  = new WsBackgroundThread(this);
                 .setContextClassLoader(
                         this.getClass().getClassLoader());
                 .setDaemon(true);
                 .start();
             }
             .add(process);
         }
     }
 
 
     public void unregister(BackgroundProcess process) {
         synchronized () {
             .remove(process);
             if ( != null && .size() == 0) {
                 .halt();
                  = null;
             }
         }
     }
 
 
     private void process() {
         Set<BackgroundProcesscurrentProcesses = new HashSet<BackgroundProcess>();
         synchronized () {
             currentProcesses.addAll();
         }
         for (BackgroundProcess process : currentProcesses) {
             try {
                 process.backgroundProcess();
             } catch (Throwable t) {
                 ExceptionUtils.handleThrowable(t);
                 ..backgroundProcessFailed(t);
             }
         }
     }
 
 
     private static class WsBackgroundThread extends Thread {
 
         private final BackgroundProcessManager manager;
         private volatile boolean running = true;
 
         public WsBackgroundThread(BackgroundProcessManager manager) {
            setName("WebSocket background processing");
            this. = manager;
        }
        @Override
        public void run() {
            while () {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // Ignore
                }
                .process();
            }
        }
        public void halt() {
            setName("WebSocket background processing - stopping");
             = false;
        }
    }
New to GrepCode? Check out our FAQ X