Good example of livelock?

后端 未结 11 1988
后悔当初
后悔当初 2021-01-29 18:44

I understand what livelock is, but I was wondering if anyone had a good code-based example of it? And by code-based, I do not mean \"two people trying to get p

11条回答
  •  北恋
    北恋 (楼主)
    2021-01-29 19:00

    One example here might be using a timed tryLock to obtain more than one lock and if you can't obtain them all, back off and try again.

    boolean tryLockAll(Collection locks) {
      boolean grabbedAllLocks = false;
      for(int i=0; i= 0; j--) {
            lock.unlock();
          }
        }
      }
    }
    

    I could imagine such code would be problematic as you have lots of threads colliding and waiting to obtain a set of locks. But I'm not sure this is very compelling to me as a simple example.

提交回复
热议问题