0

There was a Study of Common Pitfalls in Simple Multi-Threaded Programs conducted by the University of Washington. The detected errors were divided into 3 categories: Data Races, Deadlock and Miscellaneous.

In the Miscellaneous category, there was an error I don't understand:

Unnecessary use of interrupt disabling and lock acquisition and release

What does interrupt disabling and lock acquisition and release mean?
Why it shouldn't be used together?

4

1 回答 1

1

不必要地使用中断禁用

public void run(){

    while(true){
    //some deep logic 
       try{
          Thread.sleep(5000);
       }catch(InterruptedException){
          //eating away the exception without taking any steps
       }
    }
}

所以现在运行这个逻辑的线程不能被中断,因为它正在吃掉异常,所以它被称为中断禁用,唯一的方法是终止这个非常糟糕的程序,因为它会使数据结构处于不正确的状态。

不必要地使用锁的获取和释放

class Test{

    private int counter = 0;

    public synchronized void increment(){ counter++; }

    public synchronized void decrement(){ counter--;}

    //Unnecessary placement of synchronized block here
    public synchronized void printString(){
        //this method does not use counter so no need to use synchronized block
    }
}
于 2013-08-30T22:38:41.303 回答