简体   繁体   English

InterruptedException没有捕获?

[英]InterruptedException not catch?

I'm trying to implement the Raft Consensus Algorithm for a Distributed System Project, in particular now I'm concentrating about Leader Election Algorithm. 我正在尝试为分布式系统项目实施Raft共识算法,特别是现在我专注于领导者选举算法。 Essentially, there are 3 states: 基本上,有3种状态:

  1. Follower 信徒
  2. Candidate 候选人
  3. Leader 领导

The state passage is pretty complicate if you don't know the algorithm, and I think that the only useful things to know is that each state perform a different task. 如果您不了解算法,那么状态段落非常复杂,我认为唯一有用的事情是每个州执行不同的任务。 So I implemented this classes: 所以我实现了这个类:

public class ServerStateExecutor {
    private ExecutorService executor;
    private ServerState state;
    private Future<?> future;

    public ServerStateExecutor()
    {
        executor = Executors.newSingleThreadExecutor();
        SwitchFollower();
    }

    public void ExecuteState(ServerState state)
    {
        if(future!=null) {
            future.cancel(true);
        }
        System.out.println("Submitting...");
        future = executor.submit(state);
    }

    public void SwitchFollower() {
        ExecuteState(new Follower(this));
    }

    public void SwitchCandidate() {
        ExecuteState(new Candidate(this));//if true then no Timeout
    }

    public void SwitchLeader() {
        ExecuteState(new Leader(this));
    }
}

public abstract class ServerState implements Runnable {
    protected ServerStateExecutor executor;

    public abstract void run();
    public ServerState(ServerStateExecutor executor)
    {
        this.executor = executor;
    }
}

As you can see, in my implementation when you switch from a state to another one, first you (try to) "kill" the task relative to the actual state, and then you submit the task relative to the new state. 正如您所看到的,在我的实现中,当您从状态切换到另一个状态时,首先您(尝试)相对于实际状态“杀死”任务,然后相对于新状态提交任务。

I will post a "stupid" implementation of the task performed in the Follower and Candidate states: 我将在Follower和Candidate状态中发布任务的“愚蠢”实现:

public class Follower extends ServerState {

    public Follower(ServerStateExecutor executor) {
        super(executor);
    }
    @Override
    public void run() {
        try {
            Thread.currentThread().sleep(10000);
            executor.SwitchCandidate();
        }
        catch(Exception e){System.out.println("INTERRUPTION!");}
    }
}

public class Candidate extends ServerState {

    public Candidate(ServerStateExecutor executor) {
        super(executor);
    }

    @Override
    public void run() {
        try {
            Thread.currentThread().sleep(3000);
        } catch (InterruptedException e) {
            System.out.println("Interrupted!");
        }
        executor.SwitchFollower();
    }
}

Now, as you can imagine while I execute Follower.run() the variable future is referring to the task Follower.run() . 现在,正如你可以想象的那样,当我执行Follower.run() ,变量future指的是任务Follower.run() So why if I call SwitchCandidate during Follower.run() the InterruptException thrown by future.cancel(true) is not catch by Follower.run() ? 那么,为什么如果我叫SwitchCandidate期间Follower.run()InterruptException通过抛出future.cancel(true)不被抓到Follower.run()

In other words, why Follower.run() doesn't catch the interruption thrown by itself? 换句话说,为什么Follower.run()没有捕获自身抛出的中断?

Interrupting a Thread 中断线程

If any thread is in sleeping or waiting state (ie sleep() or wait() is invoked), calling the interrupt() method on the thread, breaks out the sleeping or waiting state throwing InterruptedException. 如果任何线程处于休眠或等待状态(即调用sleep()或wait()),则在线程上调用interrupt()方法会中断抛出InterruptedException的休眠或等待状态。 If the thread is not in the sleeping or waiting state, calling the interrupt() method performs normal behaviour and doesn't interrupt the thread but sets the interrupt flag to true. 如果线程未处于休眠或等待状态,则调用interrupt()方法将执行正常行为,并且不会中断线程,但会将中断标志设置为true。

Example of doing So : 这样做的例子:

class TestInterruptingThread1 extends Thread{  
public void run(){  
try{  
Thread.sleep(1000);  
System.out.println("task");  
}catch(InterruptedException e){  
throw new RuntimeException("Thread interrupted..."+e);  
}  

}  

public static void main(String args[]){  
TestInterruptingThread1 t1=new TestInterruptingThread1();  
t1.start();  
try{  
t1.interrupt();  
}catch(Exception e){System.out.println("Exception handled "+e);}  

}  
}  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Interruptedexception 的不间断捕获块 - Uninterrupted catch block for Interruptedexception 在InterruptedException的catch子句中中断线程的原因是什么? - What is the reason for interrupting the thread in the catch clause of InterruptedException? 捕获InterruptedException时JVM是否需要退出 - Does JVM need to exit when catch InterruptedException 如果在线程的InterruptedException的catch块中使用“返回”,该返回哪里? - Where does “return” returns if used inside the catch block of an InterruptedException in a thread? 在哪里使用synchronized块捕获Object.wait()的InterruptedException? - Where to catch InterruptedException for Object.wait() with synchronized block? 为什么要捕获InterruptedException来调用Thread.currentThread.interrupt()? - Why would you catch InterruptedException to call Thread.currentThread.interrupt()? Java:当被另一个线程中断时,如何在线程上捕获InterruptedException? - Java: How do I catch InterruptedException on a thread, when interrupted by another thread? Java:当在 catch 块中抛出 InterruptedException 时,finally-block 似乎被执行了两次 - Java: finally-block seems to be executed twice when InterruptedException is thrown in catch block 无限的Thread.sleep而lambda中的循环不需要'catch(InterruptedException)' - 为什么不呢? - Thread.sleep inside infinite while loop in lambda doesn't require 'catch (InterruptedException)' - why not? 我希望我的线程处理中断,但我无法捕获 InterruptedException,因为它是一个已检查的异常 - I want my thread to handle interruption, but I can't catch InterruptedException because it is a checked exception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM