Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*-
   * See the file LICENSE for redistribution information.
   *
   * Copyright (c) 2002, 2013 Oracle and/or its affiliates.  All rights reserved.
   *
   */
  
  package com.sleepycat.je.utilint;
  
 
A daemon thread. Also see StoppableThread for an alternative daemon construct.
 
 public abstract class DaemonThread
     implements DaemonRunnerRunnableExceptionListenerUser {
 
     private static final int JOIN_MILLIS = 10;
     private long waitTime;
     private Object synchronizer = new Object();
     private Thread thread;
     protected String name;
     protected int nWakeupRequests;
     public static boolean stifleExceptionChatter = false;
 
     /* Fields shared between threads must be 'volatile'. */
     private volatile boolean shutdownRequest = false;
     private volatile boolean paused = false;
 
     /* This is not volatile because it is only an approximation. */
     private boolean running = false;
 
     /* Fields for DaemonErrorListener, enabled only during testing. */
     protected final EnvironmentImpl envImpl;
     private static final String ERROR_LISTENER = "setErrorListener";
     /* Logger used in DaemonThread's subclasses. */
     protected final Logger logger;
 
     public DaemonThread(final long waitTime,
                         final String name,
                         final EnvironmentImpl envImpl) {
         this. = waitTime;
         String nodeName = envImpl.getNodeName();
         if (nodeName == null) {
             this. = name;
         } else {
             this. = name + " (" + nodeName + ")";
         }
         this. = envImpl;
         this. = createLogger();
         envImpl.registerExceptionListenerUser(this);
     }
 
     public void setExceptionListener(ExceptionListener exceptionListener) {
         this. = exceptionListener;
     }
 
     protected Logger createLogger() {
         return LoggerUtils.getLogger(getClass());
     }

    
For testing.
 
         return ;
     }

    
For testing.
 
     public Thread getThread() {
         return ;
     }

    
If run is true, starts the thread if not started or unpauses it if already started; if run is false, pauses the thread if started or does nothing if not started.
 
     public void runOrPause(boolean run) {
         if (run) {
              = false;
             if ( != null) {
                 wakeup();
             } else {
                  = new Thread(this);
                .setDaemon(true);
                .start();
            }
        } else {
             = true;
        }
    }
    public void requestShutdown() {
         = true;
    }

    
Requests shutdown and calls join() to wait for the thread to stop.
    public void shutdown() {
        if ( != null) {
             = true;
            while (.isAlive()) {
                synchronized () {
                    .notifyAll();
                }
                try {
                    .join();
                } catch (InterruptedException e) {
                    /*
                     * Klockwork - ok
                     * Don't say anything about exceptions here.
                     */
                }
            }
             = null;
        }
    }
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("<DaemonThread name=\"").append().append("\"/>");
        return sb.toString();
    }
    public void wakeup() {
        if (!) {
            synchronized () {
                .notifyAll();
            }
        }
    }
    public void run() {
        while (!) {
            try {
                /* Do a unit of work. */
                int numTries = 0;
                long maxRetries = nDeadlockRetries();
                while (numTries <= maxRetries &&
                       ! &&
                       !) {
                    try {
                        ++;
                         = true;
                        onWakeup();
                        break;
                    } catch (LockConflictException e) {
                    } finally {
                         = false;
                    }
                    numTries++;
                }
                /* Wait for notify, timeout or interrupt. */
                if (!) {
                    synchronized () {
                        if ( == 0 || ) {
                            .wait();
                        } else {
                            .wait();
                        }
                    }
                }
            } catch (InterruptedException e) {
                if ( != null) {
                    .exceptionThrown
                        (DbInternal.makeExceptionEvent(e));
                }
                if (!) {
                    .info
                        ("Shutting down " + this + " due to exception: " + e);
                }
                 = true;
                assert checkErrorListener(e);
            } catch (Exception e) {
                if ( != null) {
                    .exceptionThrown
                        (DbInternal.makeExceptionEvent(e));
                }
                if (!) {
                    /*
                     * If the exception caused the environment to become
                     * invalid, then shutdownRequest will have been set to true
                     * by EnvironmentImpl.invalidate, which is called by the
                     * EnvironmentFailureException ctor.
                     */
                    .log(.,
                               this.toString() + " caught exception, " + e +
                               ( ? " Exiting" : " Continuing"),
                               e);
                }
                assert checkErrorListener(e);
            } catch (Error e) {
                assert checkErrorListener(e);
                .invalidate(e); /* [#21929] */
                /*
                 * Since there is no uncaught exception handler (yet) we
                 * shutdown the thread here and log the exception.
                 */
                 = true;
                .log(."Error caught in " + thise);
            }
        }
    }

    
If Daemon Thread throws errors and exceptions, this function will catch it and throw a EnvironmentFailureException, and fail the test. Only used during testing.
    public boolean checkErrorListener(Throwable e) {
        if (Boolean.getBoolean()) {
            if (!) {
                .severe( + " " + LoggerUtils.getStackTrace(e));
            }
            new EnvironmentFailureException
                (.,
                 "Daemon thread failed during testing"e);
        }
        return true;
    }

    
Returns the number of retries to perform when Deadlock Exceptions occur.
    protected long nDeadlockRetries() {
        return 0;
    }

    
onWakeup is synchronized to ensure that multiple invocations of the DaemonThread aren't made.
    abstract protected void onWakeup()
        throws DatabaseException;

    
Returns whether shutdown has been requested. This method should be used to to terminate daemon loops.
    protected boolean isShutdownRequested() {
        return ;
    }

    
Returns whether the daemon is currently paused/disabled. This method should be used to to terminate daemon loops.
    protected boolean isPaused() {
        return ;
    }

    
Returns whether the onWakeup method is currently executing. This is only an approximation and is used to avoid unnecessary wakeups.
    public boolean isRunning() {
        return ;
    }

    
For unit testing.
    public int getNWakeupRequests() {
        return ;
    }
New to GrepCode? Check out our FAQ X