Domanda

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?

È stato utile?

Soluzione

Unnecessary use of interrupt disabling

public void run(){

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

So now the thread running this logic cannot be interrupted as it is eating away exceptions so it is called interrupt disabling, the only way to stop this would be to kill the program which is very bad as it can leave data structures in improper state.

Unnecessary use of lock acquisition and release

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
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top