Java concurrent programming-Balking in synchronous mode

Balking in synchronous mode

Balking mode is used when a thread finds that another thread or this thread has already done a certain thing, then this thread doesn’t need to do it any more
, just end and return

public class MonitorService {
    
    
	// 用来表示是否已经有线程已经在执行启动了
	private volatile boolean starting;
	public void start() {
    
    
			log.info("尝试启动监控线程...");
			synchronized (this) {
    
    
			if (starting) {
    
    
				return;
			}
			starting = true;
		}
		// 真正启动监控线程...
	}
}

Guess you like

Origin blog.csdn.net/e891377/article/details/109158442