was unable to stop the application console

Problem Description:

  Console was unable to stop the application, can only be stopped by stopping the server the way;

Code:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.LinkedBlockingQueue;

/**
 * @className MessageReceiver
 * @Desc 消息接收处理
 * @Author HZ
 * @Date 2019/11/25 9:39
 * @Version 1.0
 */
public class MessageReceiver implements Runnable{

    private final static Logger LOGGER = LoggerFactory.getLogger(MessageReceiver.class);

    /**
     * 通信消息队列
     * */
    private LinkedBlockingQueue<String> messageQueue = new LinkedBlockingQueue<>(); 

    / ** 
     * thread name 
     * * / 
    Private  Final  static String threadName = "the MessageReceiver" ; 

    / ** 
     * operation flags 
     * * / 
    Private  volatile  boolean isRunning = to true ; 

    / ** 
     * Thread 
     * * / 
    Private the Thread messageQueueThread; 

    / ** 
     * lock as dormancy and wake 
     * * / 
    Private Object lock = new new Object (); 

    / ** 
     * @title 
     * @Description initialization 
     * @author HZ 
     * @Param
     @Return * 
     * @date: 2019/11/25 10:59 
     * / 
    public  void the init () {
         the this .messageQueueThread = new new the Thread ( the this , threadName);
         the this .messageQueueThread.start (); 
    } 

    / ** 
     * @title Destory 
     * @Description destroy stops 
     * lock.notifyAll (); first wake messageQueueThread thread! ! ! 
     * This.messageQueueThread.join (); executing the thread waits messageQueueThread 
     * @author HZ 
     * @Param [] 
     * @return void 
     * @date: 2019/11/25 10:59 
     * / 
    public  void Destory () {
         the this .isRunning = false; 

        The synchronized (Lock) { 
            lock.notifyAll (); 
            logger.info ( "wake-up force messageQueueThread" ); 
        } 

        IF ( the this .messageQueueThread =! Null ) {
             the try {
                 the this .messageQueueThread.join (); 
            } the catch (InterruptedException E) { 
                logger.info ( "... close failed error message thread {} =" , E); 
            } 
        } 
        logger.info ( "the destroy MessageDatacommReceiver Success" ); 
    } 

    / **
     @Title pushIntoQueue * 
     * @Description objects into the queue 
     * @author HZ 
     * @Param [STR] 
     * @return void 
     * @date: 2019/11/25 11:01 
     * / 
    public  void pushIntoQueue (String STR) {
         the try {
             the synchronized (Lock) {
                 the this .messageQueue.put (STR); 
                logger.debug ( "wake messageQueueThread start thread" ); 
                lock.notifyAll (); 
            } 
        } the catch (InterruptedException E) { 
            LOGGER.error ( "objects into the queue ... {error} = " , E); 
        } 
    } 

    / ** 
     * @title RUN
     * @Description main thread method 
     * @author HZ 
     * @Param [] 
     * @return void 
     * @date: 2019/11/25 11:01 
     * / 
    @Override 
    public  void RUN () {
         the while ( the this .isRunning) {
             the try {
                 the synchronized (Lock) {
                     IF ( the this .messageQueue.isEmpty ()) { 
                        lock.wait (); 
                        logger.debug ( "messageQueueThread thread wakes up" ); 
                    } the else {
                         // the TODO business method
                     } 
                } 
            } the catch (Exception E) {
                 // exception is caught, prevent the thread die 
                LOGGER.error ( "messageQueueThread unanticipated abnormalities occurred" , E); 
            } 
        } 
    } 
}

 

Functional Description:

  MessageReceiver called when instantiating the class init () method, create and start a thread messageQueueThread

  Stop relying on messageQueueThread threads run mark isRunning control

  Manufacturer pushIntoQueue method by calling the event to be processed into the blocking queue messageQueue, notifyAll call notification while acquiring data fetch messageQueueThread

  messageQueueThread cycles to obtain data from the blocking queue messageQueue not destory value acquired call wait method is called obstructive object is destroyed method MessageReceiver

problem causes:

  destory () does not call lock.notifyAll (); This code

  Results in a call this.messageQueueThread.join (); When no execution method messageQueueThread been blocked, the object will not be destroyed, resources can not be released

        the synchronized (Lock) { 
            lock.notifyAll (); 
            logger.info ( "wake-up force messageQueueThread" ); 
        }

 

Reference documents:

https://www.cnblogs.com/yanze/p/10032363.html

https://blog.csdn.net/jiangguilong2000/article/details/11617529

Guess you like

Origin www.cnblogs.com/lyrb/p/11926752.html