How can you ensure in java that a block of code can not be interrupted by any other thread

前端 未结 11 1556
灰色年华
灰色年华 2021-01-04 10:02

exampl:

new Thread(new Runnable() {
  public void run() {
    while(condition) {

      *code that must not be interrupted*

      *some more code*
    }
  }         


        
11条回答
  •  遥遥无期
    2021-01-04 10:59

    Before a thread is interrupted, security manager's checkAccess() method is called. Implement your own security manager, call System.setSecurityManager to install it and make sure it doesn't let any other thread interrupt you while it is in critical section.

提交回复
热议问题