即使在java中发生异常,线程执行仍将继续
我在下面的代码中有一个线程的框架。 我使用了一个简单的倒计时闩锁。 我陷入了这样一种情况:线程1依赖于线程2来完成。如果没有异常,代码将正常运行。 但例外的可能性更大。 如果线程2或任何线程中发生异常,我希望所有线程停止执行。即使在异常发生后,我的独立线程仍会继续执行。我还在线程1中使用了一个标志来检查线程2是否发生异常 我特意将除法为零的异常作为一个例子来测试是否有例外。 我找不到解决这个问题的办法。 请帮帮我即使在java中发生异常,线程执行仍将继续,java,multithreading,exception-handling,countdownlatch,Java,Multithreading,Exception Handling,Countdownlatch,我在下面的代码中有一个线程的框架。 我使用了一个简单的倒计时闩锁。 我陷入了这样一种情况:线程1依赖于线程2来完成。如果没有异常,代码将正常运行。 但例外的可能性更大。 如果线程2或任何线程中发生异常,我希望所有线程停止执行。即使在异常发生后,我的独立线程仍会继续执行。我还在线程1中使用了一个标志来检查线程2是否发生异常 我特意将除法为零的异常作为一个例子来测试是否有例外。 我找不到解决这个问题的办法。 请帮帮我 import java.util.concurrent.CountDownLatc
import java.util.concurrent.CountDownLatch;
public class MainThread extends Thread{
static boolean flag=false;
final static CountDownLatch latch1= new CountDownLatch(1);
final static CountDownLatch latch2= new CountDownLatch(1);
final static CountDownLatch latch3= new CountDownLatch(3);
static MainThread t1;
static MainThread t2;
static MainThread t3;
static MainThread t4;
static MainThread t5;
public static void main(String args[]){
t1 = new MainThread(){
public void run(){
System.out.println("Waiting for Thread 2");
try {
System.out.println("THis iss before the thread 2 starts its for loop.");
latch2.countDown();
t3.start();
t4.start();
t5.start();
System.out.println("waiting for thread 2 to countdown");
latch1.await();
if(flag==true){
System.out.println("successful.");
}
else{
System.out.println("error.");
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("All the dependencies resolved.");
System.out.println("Waiting for the remaining threads to complete their work.");
try {
latch3.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("All the threads have finished doing their work. Exiting now...");
}
};
Thread t2 = new MainThread(){
public void run(){
System.out.println("Before Starting for loop");
try {
System.out.println("waiting for thread 1 to countdown latch2");
latch2.await();
System.out.println("Starting for loop");
for(int i=0;i<5;i++){
System.out.println("iteration: "+i);
try {
Thread.sleep(5);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
int x=1/0;
latch1.countDown();
System.out.println("countdown by thread2 for latch 1 done.");
flag=true;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
latch1.countDown();
}
}
};
t3 = new MainThread(){
public void run(){
System.out.println("Running Thread 3");
for(int i=0;i<10;i++){
System.out.println("iteration: "+i+ " "+t3.getName());
try {
Thread.sleep(5);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
latch3.countDown();
}
};
t4 = new MainThread(){
public void run(){
System.out.println("Running Thread 4");
for(int i=0;i<10;i++){
System.out.println("iteration: "+i+ " "+t4.getName());
try {
Thread.sleep(5);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
latch3.countDown();
}
};
t5 = new MainThread(){
public void run(){
System.out.println("Running Thread 5");
for(int i=0;i<10;i++){
System.out.println("iteration: "+i+ " "+t5.getName());
try {
Thread.sleep(5);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
latch3.countDown();
}
};
t1.start();
t2.start();
}
}
你想过使用线程池吗?如果您可以修改使用线程池的要求,在异常情况下,您可以强制关闭线程池,因此所有线程都将停止。Java没有提供任何直接方法来停止正在运行的线程的执行,但它提供了中断线程的方法。尽管如此,它仍然不能保证停止线程的执行,它只是将该线程的中断标志设置为true,它依赖于线程来检查标志并抛出中断异常。这是停止线程执行的唯一优雅方法。
您可以通过调用handle.interrupt()中断另一个线程。好的,使用当前代码,您需要获得正确的透视图。请自行定义闩锁变量,否则会使您感到困惑。假设以下是您的需求:仅当线程2执行成功时才继续执行其他线程 以下是更新后的代码:
import java.util.concurrent.CountDownLatch;
public class MainThread extends Thread{
static boolean flag=false;
final static CountDownLatch waitForThread2ToFinish= new CountDownLatch(1);
final static CountDownLatch waitForStartSignalFromThread1= new CountDownLatch(1);
final static CountDownLatch latchForAllOtherThreads= new CountDownLatch(3);
static MainThread t1;
static MainThread t2;
static MainThread t3;
static MainThread t4;
static MainThread t5;
public static void main(String args[]){
t1 = new MainThread(){
public void run(){
try {
System.out.println("Waiting for Thread 2 to finish");
waitForStartSignalFromThread1.countDown();
waitForThread2ToFinish.await();
if(flag==true){
System.out.println("Successful.");
t3.start();
t4.start();
t5.start();
System.out.println("All the dependencies resolved.");
System.out.println("Waiting for the remaining threads to complete their work.");
try {
latchForAllOtherThreads.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("All the threads have finished doing their work. Exiting now...");
}
else{
System.out.println("Error.");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
Thread t2 = new MainThread(){
public void run(){
System.out.println("Before Starting for loop");
try {
System.out.println("waiting for thread 1 to countdown latch2");
waitForStartSignalFromThread1.await();
System.out.println("Starting for loop");
for(int i=0;i<5;i++){
System.out.println("iteration: "+i);
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
int x=1/0;
System.out.println("countdown by thread2 for latch 1 done.");
flag=true;
} catch (Exception e) {
e.printStackTrace();
}
finally{
waitForThread2ToFinish.countDown();
}
}
};
t3 = new MainThread(){
public void run(){
System.out.println("Running Thread 3");
for(int i=0;i<10;i++){
System.out.println("iteration: "+i+ " "+t3.getName());
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
latchForAllOtherThreads.countDown();
}
};
t4 = new MainThread(){
public void run(){
System.out.println("Running Thread 4");
for(int i=0;i<10;i++){
System.out.println("iteration: "+i+ " "+t4.getName());
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
latchForAllOtherThreads.countDown();
}
};
t5 = new MainThread(){
public void run(){
System.out.println("Running Thread 5");
for(int i=0;i<10;i++){
System.out.println("iteration: "+i+ " "+t5.getName());
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
latchForAllOtherThreads.countDown();
}
};
t1.start();
t2.start();
}
}
import java.util.concurrent.CountDownLatch;
公共类主线程扩展线程{
静态布尔标志=false;
最终静态CountDownLatch waitForThread2ToFinish=新的CountDownLatch(1);
最终静态倒计时闩锁waitForStartSignalFromThread1=新倒计时闩锁(1);
最终静态倒计时闩锁闩锁forallotherthreads=新倒计时闩锁(3);
静态主线程t1;
静态主线程t2;
静态主线程t3;
静态主线程t4;
静态主线程t5;
公共静态void main(字符串参数[]){
t1=新的主线程(){
公开募捐{
试一试{
System.out.println(“等待线程2完成”);
waitForStartSignalFromThread1.countDown();
waitForThread2ToFinish.wait();
如果(标志==真){
System.out.println(“成功”);
t3.start();
t4.开始();
t5.开始();
System.out.println(“所有依赖项已解决”);
System.out.println(“等待剩余线程完成其工作”);
试一试{
latchForAllOtherThreads.await();
}捕捉(中断异常e){
e、 printStackTrace();
}
System.out.println(“所有线程都完成了它们的工作。现在退出…”);
}
否则{
System.out.println(“错误”);
}
}捕捉(中断异常e){
e、 printStackTrace();
}
}
};
线程t2=新的主线程(){
公开募捐{
System.out.println(“开始循环前”);
试一试{
System.out.println(“等待线程1倒数锁存2”);
waitForStartSignalFromThread1.wait();
System.out.println(“为循环启动”);
对于(int i=0;iI认为使用闩锁比使用池更简单。我可以尝试使用池。有没有仅使用上述代码的解决方案?我现在没有IDE可供使用,但可以尝试的方法之一是:1.使用易失性布尔变量并将其设置为false 2。如果发生异常,将其值更改为true 3。在所有线程中,对于每次迭代在进行任何处理(在您打印消息的情况下)之前,请检查此变量。如果值为true,请中断循环。我将尝试此操作。ThanksThread 3、4和5是独立线程。我曾想过同时运行它们。异常处理是我在这里面临的唯一问题。@iampitre然后,如果(flag==true)
条件。是否要抑制异常?是的,我可以这样做。谢谢。但我仍然没有得到我想要的。
import java.util.concurrent.CountDownLatch;
public class MainThread extends Thread{
static boolean flag=false;
final static CountDownLatch waitForThread2ToFinish= new CountDownLatch(1);
final static CountDownLatch waitForStartSignalFromThread1= new CountDownLatch(1);
final static CountDownLatch latchForAllOtherThreads= new CountDownLatch(3);
static MainThread t1;
static MainThread t2;
static MainThread t3;
static MainThread t4;
static MainThread t5;
public static void main(String args[]){
t1 = new MainThread(){
public void run(){
try {
System.out.println("Waiting for Thread 2 to finish");
waitForStartSignalFromThread1.countDown();
waitForThread2ToFinish.await();
if(flag==true){
System.out.println("Successful.");
t3.start();
t4.start();
t5.start();
System.out.println("All the dependencies resolved.");
System.out.println("Waiting for the remaining threads to complete their work.");
try {
latchForAllOtherThreads.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("All the threads have finished doing their work. Exiting now...");
}
else{
System.out.println("Error.");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
Thread t2 = new MainThread(){
public void run(){
System.out.println("Before Starting for loop");
try {
System.out.println("waiting for thread 1 to countdown latch2");
waitForStartSignalFromThread1.await();
System.out.println("Starting for loop");
for(int i=0;i<5;i++){
System.out.println("iteration: "+i);
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
int x=1/0;
System.out.println("countdown by thread2 for latch 1 done.");
flag=true;
} catch (Exception e) {
e.printStackTrace();
}
finally{
waitForThread2ToFinish.countDown();
}
}
};
t3 = new MainThread(){
public void run(){
System.out.println("Running Thread 3");
for(int i=0;i<10;i++){
System.out.println("iteration: "+i+ " "+t3.getName());
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
latchForAllOtherThreads.countDown();
}
};
t4 = new MainThread(){
public void run(){
System.out.println("Running Thread 4");
for(int i=0;i<10;i++){
System.out.println("iteration: "+i+ " "+t4.getName());
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
latchForAllOtherThreads.countDown();
}
};
t5 = new MainThread(){
public void run(){
System.out.println("Running Thread 5");
for(int i=0;i<10;i++){
System.out.println("iteration: "+i+ " "+t5.getName());
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
latchForAllOtherThreads.countDown();
}
};
t1.start();
t2.start();
}
}