@Test public void test() { LazyInitializer<Integer> lazy = new LazyInitializer<>(new Callable<Integer>() { private int i = 0; @Override public Integer call() throws Exception { return i++; } }); Assert.assertEquals((Integer)0, lazy.get()); Assert.assertEquals((Integer)0, lazy.get()); }
public T get() { if(this.instance == null) { while(lock.tryLock()) { try { if(this.instance == null) { return this.instance = factory.call(); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } catch (Exception e) { throw new RuntimeException(e); } finally { lock.unlock(); } } } return instance; }
LazyInitializer { public T get() { if(this.instance == null) { while(lock.tryLock()) { try { if(this.instance == null) { return this.instance = factory.call(); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } catch (Exception e) { throw new RuntimeException(e); } finally { lock.unlock(); } } } return instance; } }
LazyInitializer { public T get() { if(this.instance == null) { while(lock.tryLock()) { try { if(this.instance == null) { return this.instance = factory.call(); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } catch (Exception e) { throw new RuntimeException(e); } finally { lock.unlock(); } } } return instance; } LazyInitializer(Callable<T> factory); }
LazyInitializer { public T get() { if(this.instance == null) { while(lock.tryLock()) { try { if(this.instance == null) { return this.instance = factory.call(); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } catch (Exception e) { throw new RuntimeException(e); } finally { lock.unlock(); } } } return instance; } LazyInitializer(Callable<T> factory); T get(); }
LazyInitializer { public T get() { if(this.instance == null) { while(lock.tryLock()) { try { if(this.instance == null) { return this.instance = factory.call(); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } catch (Exception e) { throw new RuntimeException(e); } finally { lock.unlock(); } } } return instance; } LazyInitializer(Callable<T> factory); T get();  }