Code example for AtomicBoolean

Methods: compareAndSet, set

0
     */ 
    public void heartbeat() { 
        final ReentrantLock lock = this.lock;
        lock.lock();
        try { 
            if (isChecking.compareAndSet(false, true)) {
                MySQLDetector detector = this.detector;
                if (detector == null || detector.isQuit() || detector.isClosed()) {
                    try { 
                        detector = factory.make(this);
                    } catch (Throwable e) {
                        LOGGER.warn(source.getConfig().toString(), e);
                        setError(null); 
                        return; 
                    } 
                    this.detector = detector;
                } else { 
                    detector.heartbeat();
                } 
            } else { 
                MySQLDetector detector = this.detector;
                if (detector != null) {
                    if (detector.isQuit() || detector.isClosed()) {
                        isChecking.compareAndSet(true, false);
                    } else if (detector.isHeartbeatTimeout()) {
                        setTimeout(detector);
                    } 
                } 
            } 
        } finally { 
            lock.unlock();
        } 
    } 
 
    public void setResult(int result, MySQLDetector detector, boolean isTransferError) {
        switch (result) {
        case OK_STATUS:
            setOk(detector);
            break; 
        case ERROR_STATUS:
            if (detector.isQuit()) {
                isChecking.set(false);
            } else { 
                if (isTransferError) {
                    detector.close();
                } 
                setError(detector);
            } 
            break; 
        } 
    } 
 
    private void setOk(MySQLDetector detector) {
        recorder.set(detector.lastReadTime() - detector.lastWriteTime());
        switch (status) {
        case TIMEOUT_STATUS:
            this.status = INIT_STATUS;
            this.errorCount = 0;
            this.isChecking.set(false);
            if (isStop.get()) {
                detector.quit();
            } else { 
                heartbeat();// timeout, heart beat again 
            } 
            break; 
        default: 
            this.status = OK_STATUS;
            this.errorCount = 0;
            this.isChecking.set(false);
            if (isStop.get()) {
                detector.quit();
            } 
        } 
    } 
 
    private void setError(MySQLDetector detector) {
        if (++errorCount < maxRetryCount) {
            isChecking.set(false);
            if (detector != null && isStop.get()) {
                detector.quit();
            } else { 
                heartbeat(); // error count not enough, heart beat again 
            } 
        } else { 
            this.status = ERROR_STATUS;
            this.errorCount = 0;
            this.isChecking.set(false);
            try { 
                switchSource("ERROR"); 
            } finally { 
                if (detector != null && isStop.get()) {
                    detector.quit();