日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

java stop graceful

發(fā)布時(shí)間:2025/3/8 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java stop graceful 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>

import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean;import sun.misc.Signal; import sun.misc.SignalHandler;public class JavaStopGraceFully {public static int len = 5;static ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<>(1000);static ExecutorService executorService = Executors.newFixedThreadPool(3);static volatile AtomicBoolean stopPut = new AtomicBoolean(false);static volatile AtomicBoolean stopPoll = new AtomicBoolean(false);static volatile AtomicBoolean readyExitFlag = new AtomicBoolean(false);public static void main(String[] args) throws InterruptedException {executorService.submit(new Runnable() {@Overridepublic void run() {while (true) {for (int i = 0; i < len + 1; i++) {try {queue.put(i);} catch (InterruptedException e) {e.printStackTrace();}}try {TimeUnit.SECONDS.sleep(len);} catch (InterruptedException e) {e.printStackTrace();}if (stopPut.get()) {break;}}System.out.println("exit put thread.");}});executorService.submit(new Runnable() {@Overridepublic void run() {while (true) {Integer ele = queue.poll();System.out.println(ele);try {TimeUnit.SECONDS.sleep(1L);} catch (InterruptedException e) {e.printStackTrace();}if (stopPoll.get()) {break;}}System.out.println("exit poll thread.");}});executorService.submit(new Runnable() {@Overridepublic void run() {while (true) {if (!stopPut.get()) {continue;}if (stopPut.get()) {if (0 == queue.size()) {stopPoll.set(true);readyExitFlag.set(true);}}if (readyExitFlag.get()) {break;}try {TimeUnit.SECONDS.sleep(1L);} catch (InterruptedException e) {e.printStackTrace();}}System.out.println("exit loop thread.");if (readyExitFlag.get()) {System.out.println("exit gracefully.");System.exit(0);}}});SignalHandler sh = new SignalHandler() {@Overridepublic void handle(Signal signal) {System.out.println(signal.getName() + ".." + signal.getNumber());if (signal.getName().equals("INT")|| signal.getName().equals("TERM")) {if (readyExitFlag.get()) {System.out.println("exit gracefully.");System.exit(0);} else {if (stopPut.get()) {System.out.println("wait for pop done.");} else {stopPut.set(true);}}}}};Signal.handle(new Signal(System.getProperties().getProperty("os.name").toLowerCase().startsWith("win") ? "INT" : "TERM"), sh);} }

轉(zhuǎn)載于:https://my.oschina.net/corleone/blog/1537824

總結(jié)

以上是生活随笔為你收集整理的java stop graceful的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。