/*
 * 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.rocketmq.common;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.rocketmq.common.constant.LoggerName;
import org.apache.rocketmq.logging.InternalLogger;
import org.apache.rocketmq.logging.InternalLoggerFactory;

public abstract class ServiceThread implements Runnable {
    private static final InternalLogger log = InternalLoggerFactory.getLogger(LoggerName.COMMON_LOGGER_NAME);

    private static final long JOIN_TIME = 90 * 1000;

    protected final Thread thread;
    protected final CountDownLatch2 waitPoint = new CountDownLatch2(1);
    protected volatile AtomicBoolean hasNotified = new AtomicBoolean(false);
    protected volatile boolean stopped = false;

    public ServiceThread() {
        this.thread = new Thread(this, this.getServiceName());
    }

    public abstract String getServiceName();

    public void start() {
        this.thread.start();
    }

    public void shutdown() {
        this.shutdown(false);
    }

    public void shutdown(final boolean interrupt) {
        this.stopped = true;
        log.info("shutdown thread " + this.getServiceName() + " interrupt " + interrupt);

        if (hasNotified.compareAndSet(false, true)) {
            waitPoint.countDown(); // notify
        }

        try {
            if (interrupt) {
                this.thread.interrupt();
            }

            long beginTime = System.currentTimeMillis();
            if (!this.thread.isDaemon()) {
                this.thread.join(this.getJointime());
            }
            long eclipseTime = System.currentTimeMillis() - beginTime;
            log.info("join thread " + this.getServiceName() + " eclipse time(ms) " + eclipseTime + " "
                + this.getJointime());
        } catch (InterruptedException e) {
            log.error("Interrupted", e);
        }
    }

    public long getJointime() {
        return JOIN_TIME;
    }

    public void stop() {
        this.stop(false);
    }

    public void stop(final boolean interrupt) {
        this.stopped = true;
        log.info("stop thread " + this.getServiceName() + " interrupt " + interrupt);

        if (hasNotified.compareAndSet(false, true)) {
            waitPoint.countDown(); // notify
        }

        if (interrupt) {
            this.thread.interrupt();
        }
    }

    public void makeStop() {
        this.stopped = true;
        log.info("makestop thread " + this.getServiceName());
    }

    public void wakeup() {
        if (hasNotified.compareAndSet(false, true)) {
            waitPoint.countDown(); // notify
        }
    }

    protected void waitForRunning(long interval) {
        if (hasNotified.compareAndSet(true, false)) {
            this.onWaitEnd();
            return;
        }

        //entry to wait
        waitPoint.reset();

        try {
            waitPoint.await(interval, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            log.error("Interrupted", e);
        } finally {
            hasNotified.set(false);
            this.onWaitEnd();
        }
    }

    protected void onWaitEnd() {
    }

    public boolean isStopped() {
        return stopped;
    }
}

@@ -1322,22 +1322,16 @@
 otected 
-final 
 Thread t
@@ -1543,328 +1543,857 @@
     
-public ServiceT
+//Make it able to restart the t
 hread
-() %7B
 %0A    
-    this.thread = new Thread(this, this.getServiceName());%0A    %7D%0A%0A    public abstract String getServiceName();%0A%0A    public void start() %7B%0A        this.thread.start();%0A    %7D%0A%0A    public void shutdown() %7B%0A        this.shutdown(false);%0A    %7D%0A%0A    public void shutdown(final boolean interrupt) %7B
+private final AtomicBoolean started = new AtomicBoolean(false);%0A%0A    public ServiceThread() %7B%0A%0A    %7D%0A%0A    public abstract String getServiceName();%0A%0A    public void start() %7B%0A        log.info(%22Try to start service thread:%7B%7D started:%7B%7D lastThread:%7B%7D%22, getServiceName(), started.get(), thread);%0A        if (!started.compareAndSet(false, true)) %7B%0A            return;%0A        %7D%0A        stopped = false;%0A        this.thread = new Thread(this, getServiceName());%0A        this.thread.start();%0A    %7D%0A%0A    public void shutdown() %7B%0A        this.shutdown(false);%0A    %7D%0A%0A    public void shutdown(final boolean interrupt) %7B%0A        log.info(%22Try to start service thread:%7B%7D started:%7B%7D lastThread:%7B%7D%22, getServiceName(), started.get(), thread);%0A        if (!started.compareAndSet(true, false)) %7B%0A            return;%0A        %7D
 %0A   
@@ -3363,32 +3363,92 @@
 an interrupt) %7B%0A
+        if (!started.get()) %7B%0A            return;%0A        %7D%0A
         this.sto
@@ -3757,32 +3757,92 @@
 id makeStop() %7B%0A
+        if (!started.get()) %7B%0A            return;%0A        %7D%0A
         this.sto

Make ServiceThread be able to be restarted