Lazy Loading Reference : Concurrent « Collections Data Structure « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Collections Data Structure » Concurrent 




Lazy Loading Reference
        
/*
 * Copyright 2008-2010 the T2 Project ant the Others.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
//package org.t2framework.commons.util;

import java.lang.ref.WeakReference;
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.concurrent.atomic.AtomicReference;

public class LazyLoadingReference<T> {

  protected Factory<T> factory;

  protected AtomicReference<WeakReference<Future<T>>> reference = new AtomicReference<WeakReference<Future<T>>>();

  public LazyLoadingReference(Factory<T> factory) {
    this.factory = factory;
  }

  public T get() throws IllegalStateException {
    while (true) {
      WeakReference<Future<T>> ref = reference.get();
      boolean valid = true;
      if (ref == null) {
        FutureTask<T> f = new FutureTask<T>(new Callable<T>() {
          @Override
          public T call() throws Exception {
            return factory.create();
          }
        });
        ref = new WeakReference<Future<T>>(f);
        if (valid = reference.compareAndSet(null, ref)) {
          f.run();
        }
      }
      if (valid) {
        try {
          Future<T> f = ref.get();
          if (f != null) {
            return f.get();
          else {
            reference.compareAndSet(ref, null);
          }
        catch (CancellationException e) {
          reference.compareAndSet(ref, null);
        catch (ExecutionException e) {
          throw new IllegalStateException(e.getCause());
        catch (Exception e) {
          throw new IllegalStateException(e);
        }
      }
    }
  }

  public interface Factory<T> {
    create() throws CancellationException, Exception;
  }
}
-------------------------
/*
 * Copyright 2008-2009 the T2 Project ant the Others.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.t2framework.commons.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CancellationException;

import org.t2framework.commons.cache.Cache;
import org.t2framework.commons.cache.CacheFactory;
import org.t2framework.commons.cache.CacheType;

import junit.framework.TestCase;

public class LazyLoadingReferenceTest extends TestCase {

  public void test_loadSimple() throws Exception {
    LazyLoadingReference<String> target = new LazyLoadingReference<String>(
        new LazyLoadingReference.Factory<String>() {
          public String create() {
            return "hoge";
          }
        });
    assertEquals("hoge", target.get());
  }

  public void test_loadSimple2() throws Exception {
    LazyLoadingReference<Cache<String, String>> t2 = new LazyLoadingReference<Cache<String, String>>(
        new LazyLoadingReference.Factory<Cache<String, String>>() {

          @Override
          public Cache<String, String> create()
              throws CancellationException {
            Cache<String, String> c = CacheFactory
                .createCache(CacheType.DEFAULT);
            c.put("aaa""bbb");
            return c;
          }
        });
    Cache<String, String> cache = t2.get();
    assertNotNull(cache);
    assertNotNull(cache.get("aaa"));

  }

  public void test_exception() throws Exception {
    final Exception e = new Exception();
    LazyLoadingReference<String> target = new LazyLoadingReference<String>(
        new LazyLoadingReference.Factory<String>() {
          public String create() throws Exception {
            throw e;
          }
        });
    try {
      target.get();
      fail();
    catch (IllegalStateException t) {
      assertNotNull(t.getCause());
      assertTrue(t.getCause().getClass() == Exception.class);
      assertEquals(e, t.getCause());
    }
  }

  public void test_loadHeavy() throws Exception {
    final LazyLoadingReference<String> target = new LazyLoadingReference<String>(
        new LazyLoadingReference.Factory<String>() {
          public String create() {
            return "hoge";
          }
        });
    final Random random = new Random(System.currentTimeMillis());
    List<Thread> list = new ArrayList<Thread>();
    for (int i = 0; i < 20; i++) {
      Runnable r = new Runnable() {
        public void run() {
          try {
            long l = random.nextLong() 100;
            Thread.sleep(l < ? l * -: l);
            assertEquals("hoge", target.get());
          catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
      };
      Thread t = new Thread(r);
      list.add(t);
      t.start();
    }
    for (Thread t : list) {
      t.join();
    }
  }
}

   
    
    
    
    
    
    
    
  














Related examples in the same category
1.A version of Hashtable supporting concurrency for both retrievals and updates
2.A version of Hashtable that supports mostly-concurrent reading, but exclusive writing
3.Synchronized Queue
4.Concurrent Doubly LinkedList
5.Returns the parent of the specified URI.
6.A daemon thread that continuously dequeues Runnable instances from a queue and executes them.
7.Utility class that provides a lazy initialization object wrapper.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.