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

前端 未结 11 1492
灰色年华
灰色年华 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:44

    Best halfway solution would be to synchronize all threads on some common object so that no other threads are runnable while you're in the critical section.

    Other than that I do not think it's possible. And I'm quite curious as to what kind of problem that requires this type of solution ?

提交回复
热议问题