仿商城版淘宝客网站源码,公司管理系统名称大全,wordpress虚拟物品销售,北京网站制作与网站设计我写了一个Java守护进程(一个实现守护进程和Runnable的类)#xff0c;现在我遇到了以下问题#xff1a;在init()中#xff0c;我创建了一个新线程 . Thread thread new Thread(this); 在start()中我启动新线程 . thread.start() . 在运行中我做了很多不同的事情......然后发…我写了一个Java守护进程(一个实现守护进程和Runnable的类)现在我遇到了以下问题在init()中我创建了一个新线程 . Thread thread new Thread(this); 在start()中我启动新线程 . thread.start() . 在运行中我做了很多不同的事情......然后发生异常 . (在我的例子中NullPointerException . ) .现在发生的是我在run()中捕获异常并调用stop() . 在那里我调用 thread.join() 但是这永远不会完成并且不会抛出InterruptedException . 我怎么解决这个问题在run()中我在while循环中做了一些事情private Thread thread;public void init() {// if init is successful: this.thread new Thread(this);// if init is not successful: call stop()}public void run() {try{// do somethingwhile (!this.stopping){// do somthing}} catch (Exception e) {//do something and then call stop()}}public void stop() {this.stopping true;thread.join(); // catch InterruptedException if it did not work// do some more things}这就是引发异常的地方 . 停止是易失性的一旦调用停止我就将其设置为true .谢谢 -)我得到了很多答案但我仍然没有解决这个问题 . 每个人都说我不应该在我的run()或init()方法中手动调用stop() . 我的问题是当init()或run()发生异常时我想真正停止该程序 . 完成它以便程序不再运行 . 我不知道如何在不调用stop()的情况下实现这一点 . 只需等待run()完成就不起作用因为它会停止然后永远不会调用stop() . 我还是不确定如何处理异常问题 .