Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.46 KB | None | 0 0
  1.     void doAuthorized(Runnable r) throws InterruptedException {
  2.         try {
  3.             r.run();
  4.         } catch (AuthenticationException e) {
  5.             tryAuth();
  6.             doAuthorized(r);
  7.         }
  8.     }
  9.  
  10.     Lock authLock = new ReentrantLock();
  11.    
  12.     private void tryAuth() throws InterruptedException {
  13.         if (authLock.tryLock(5, TimeUnit.SECONDS)) {
  14.             try {
  15.                 doAuth();
  16.             } finally {
  17.                 authLock.unlock();
  18.             }
  19.         } else {
  20.             throw new IllegalStateException("Cannot acquire auth lock");
  21.         }
  22.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement