当前位置: 首页>>代码示例>>Java>>正文


Java PhantomReference类代码示例

本文整理汇总了Java中java.lang.ref.PhantomReference的典型用法代码示例。如果您正苦于以下问题:Java PhantomReference类的具体用法?Java PhantomReference怎么用?Java PhantomReference使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


PhantomReference类属于java.lang.ref包,在下文中一共展示了PhantomReference类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: phantom

import java.lang.ref.PhantomReference; //导入依赖的package包/类
private static void phantom() throws InterruptedException {
    //Strong Reference
    BigObject a = new BigObject();

    //Creating ReferenceQueue
    ReferenceQueue<BigObject> refQueue = new ReferenceQueue<>();

    //Creating Phantom Reference to A-type object to which 'a' is also pointing
    PhantomReference<BigObject> phantomA = new PhantomReference<>(a, refQueue);
    System.out.println("Ref in pool before GC: " + refQueue.poll());

    a = null;
    //Now, A-type object to which 'a' is pointing earlier is available for garbage collection.
    //But, this object is kept in 'refQueue' before removing it from the memory.

    a = phantomA.get();    //it always returns null

    System.gc();
    Thread.sleep(100);
    System.out.println("Ref in pool after GC: " + refQueue.poll());
}
 
开发者ID:vitaly-chibrikov,项目名称:otus_java_2017_06,代码行数:22,代码来源:Main.java

示例2: getFields

import java.lang.ref.PhantomReference; //导入依赖的package包/类
static List<Field> getFields(Class<?> clazz)
{
    if (clazz == null || clazz == PhantomReference.class || clazz == Class.class || java.lang.reflect.Member.class.isAssignableFrom(clazz))
        return emptyList();
    List<Field> fields = fieldMap.get(clazz);
    if (fields != null)
        return fields;
    fieldMap.put(clazz, fields = new ArrayList<>());
    for (Field field : clazz.getDeclaredFields())
    {
        if (field.getType().isPrimitive() || Modifier.isStatic(field.getModifiers()))
            continue;
        field.setAccessible(true);
        fields.add(field);
    }
    fields.addAll(getFields(clazz.getSuperclass()));
    return fields;
}
 
开发者ID:Netflix,项目名称:sstable-adaptor,代码行数:19,代码来源:Ref.java

示例3: FinalizableReferenceQueue

import java.lang.ref.PhantomReference; //导入依赖的package包/类
/**
 * Constructs a new queue.
 */
public FinalizableReferenceQueue() {
  // We could start the finalizer lazily, but I'd rather it blow up early.
  queue = new ReferenceQueue<Object>();
  frqRef = new PhantomReference<Object>(this, queue);
  boolean threadStarted = false;
  try {
    startFinalizer.invoke(null, FinalizableReference.class, queue, frqRef);
    threadStarted = true;
  } catch (IllegalAccessException impossible) {
    throw new AssertionError(impossible); // startFinalizer() is public
  } catch (Throwable t) {
    logger.log(Level.INFO, "Failed to start reference finalizer thread."
        + " Reference cleanup will only occur when new references are created.", t);
  }

  this.threadStarted = threadStarted;
}
 
开发者ID:s-store,项目名称:sstore-soft,代码行数:21,代码来源:FinalizableReferenceQueue.java

示例4: run

import java.lang.ref.PhantomReference; //导入依赖的package包/类
@Override
public void run() {
    while (true){
        if(phantomQueue!=null){
            PhantomReference<TraceCanReliveObj> objt=null;
            try {
                objt=(PhantomReference<TraceCanReliveObj>)phantomQueue.remove();
            }catch (InterruptedException e){
                e.fillInStackTrace();
            }
            if(objt!=null){
                System.out.println("TraceCanReliveObj is delete");
            }
        }
    }
}
 
开发者ID:sean417,项目名称:LearningOfThinkInJava,代码行数:17,代码来源:TraceCanReliveObj.java

示例5: main

import java.lang.ref.PhantomReference; //导入依赖的package包/类
public static void main(String[] args) throws InterruptedException{
    Thread t=new CheckRefQueue();
    t.setDaemon(true);
    t.start();
    phantomQueue=new ReferenceQueue<TraceCanReliveObj>();
    obj=new TraceCanReliveObj();
    PhantomReference<TraceCanReliveObj> phantomRef=new PhantomReference<TraceCanReliveObj>(obj,phantomQueue);
    obj=null;
    System.gc();
    Thread.sleep(1000);
    if(obj==null){
        System.out.println("obj 是null");
    }else {
        System.out.println("obj 可用");
    }
    System.out.println("第二次gc");
    obj=null;
    System.gc();
    Thread.sleep(1000);
    if(obj==null){
        System.out.println("obj是null");
    }else {
        System.out.println("obj 可用");
    }
}
 
开发者ID:sean417,项目名称:LearningOfThinkInJava,代码行数:26,代码来源:TraceCanReliveObj.java

示例6: FinalizableReferenceQueue

import java.lang.ref.PhantomReference; //导入依赖的package包/类
/**
 * Constructs a new queue.
 */
public FinalizableReferenceQueue() {
  // We could start the finalizer lazily, but I'd rather it blow up early.
  queue = new ReferenceQueue<Object>();
  frqRef = new PhantomReference<Object>(this, queue);
  boolean threadStarted = false;
  try {
    startFinalizer.invoke(null, FinalizableReference.class, queue, frqRef);
    threadStarted = true;
  } catch (IllegalAccessException impossible) {
    throw new AssertionError(impossible); // startFinalizer() is public
  } catch (Throwable t) {
    logger.log(
        Level.INFO,
        "Failed to start reference finalizer thread."
            + " Reference cleanup will only occur when new references are created.",
        t);
  }

  this.threadStarted = threadStarted;
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:24,代码来源:FinalizableReferenceQueue.java

示例7: phantom

import java.lang.ref.PhantomReference; //导入依赖的package包/类
private static void phantom() throws InterruptedException {
    //Strong Reference
    BigObject a = new BigObject();

    //Creating ReferenceQueue
    ReferenceQueue<BigObject> refQueue = new ReferenceQueue<>();

    //Creating Phantom Reference to A-type object to which 'a' is also pointing
    PhantomReference<BigObject> phantomA = new PhantomReference<>(a, refQueue);
    System.out.println("Big object length:" + refQueue.poll());

    a = null;
    //Now, A-type object to which 'a' is pointing earlier is available for garbage collection.
    //But, this object is kept in 'refQueue' before removing it from the memory.

    a = phantomA.get();    //it always returns null

    System.gc();
    Thread.sleep(100);
    System.out.println("Big object length:" + refQueue.poll());

}
 
开发者ID:vitaly-chibrikov,项目名称:otus_java_2017_04,代码行数:23,代码来源:Main.java

示例8: testCleanerTermination

import java.lang.ref.PhantomReference; //导入依赖的package包/类
/**
 * Test that releasing the reference to the Cleaner service allows it to be
 * be freed.
 */
@Test
void testCleanerTermination() {
    ReferenceQueue<Object> queue = new ReferenceQueue<>();
    Cleaner service = Cleaner.create();

    PhantomReference<Object> ref = new PhantomReference<>(service, queue);
    System.gc();
    // Clear the Reference to the cleaning service and force a gc.
    service = null;
    System.gc();
    try {
        Reference<?> r = queue.remove(1000L);
        Assert.assertNotNull(r, "queue.remove timeout,");
        Assert.assertEquals(r, ref, "Wrong Reference dequeued");
    } catch (InterruptedException ie) {
        System.out.printf("queue.remove Interrupted%n");
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:23,代码来源:CleanerTest.java

示例9: setupPhantomSubclassException

import java.lang.ref.PhantomReference; //导入依赖的package包/类
/**
 * Create a CleanableCase for a PhantomReference.
 * @param cleaner the cleaner to use
 * @param obj an object or null to create a new Object
 * @return a new CleanableCase preset with the object, cleanup, and semaphore
 */
static CleanableCase setupPhantomSubclassException(Cleaner cleaner, Object obj) {
    if (obj == null) {
        obj = new Object();
    }
    Semaphore s1 = new Semaphore(0);

    Cleaner.Cleanable c1 = new PhantomCleanable<Object>(obj, cleaner) {
        protected void performCleanup() {
            s1.release();
            throw new RuntimeException("Exception thrown to cleaner thread");
        }
    };

    return new CleanableCase(new PhantomReference<>(obj, null), c1, s1, true);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:22,代码来源:CleanerTest.java

示例10: FinalizableReferenceQueue

import java.lang.ref.PhantomReference; //导入依赖的package包/类
/**
   * Constructs a new queue.
   */

  public FinalizableReferenceQueue() {
    // We could start the finalizer lazily, but I'd rather it blow up early.
    queue = new ReferenceQueue<Object>();
    frqRef = new PhantomReference<Object>(this, queue);
    boolean threadStarted = false;
    try {
      startFinalizer.invoke(null, FinalizableReference.class, queue, frqRef);
      threadStarted = true;
    } catch (IllegalAccessException impossible) {
      throw new AssertionError(impossible); // startFinalizer() is public
    } catch (Throwable t) {
      logger.log(
          Level.INFO,
          "Failed to start reference finalizer thread."
+ " Reference cleanup will only occur when new references are created.",
          t);
    }
    this.threadStarted = threadStarted;
  }
 
开发者ID:antlr,项目名称:codebuff,代码行数:24,代码来源:FinalizableReferenceQueue.java

示例11: FinalizableReferenceQueue

import java.lang.ref.PhantomReference; //导入依赖的package包/类
/**
* Constructs a new queue.
*/


 public FinalizableReferenceQueue() {
 // We could start the finalizer lazily, but I'd rather it blow up early.
 queue = new ReferenceQueue<Object>();
 frqRef = new PhantomReference<Object>(this, queue);
 boolean threadStarted = false;
 try {
            startFinalizer.invoke(null, FinalizableReference.class, queue, frqRef);
            threadStarted = true;
 } catch (IllegalAccessException impossible) {
   throw new AssertionError(impossible); // startFinalizer() is public
 } catch (Throwable t) {
   logger.log(Level.INFO,
              "Failed to start reference finalizer thread." + " Reference cleanup will only occur when new references are created.", t);
 }
 this.threadStarted = threadStarted;
 }
 
开发者ID:antlr,项目名称:codebuff,代码行数:22,代码来源:FinalizableReferenceQueue.java

示例12: run

import java.lang.ref.PhantomReference; //导入依赖的package包/类
@SuppressWarnings("all")
@Override
public void run() {
	try {
		System.out.println("Waiting for GC");

		/*
		 * Aquí se bloqueara hasta que la referencia sea recolectada,
		 * otra opción es el método poll pero este no es bloqueante,
		 * sino que devuelve NULL si no hay referencias en la cola
		 */
		PhantomReference<Object> phantomReference = (PhantomReference<Object>) queue.remove();

		System.out.println("Reference processed for GC");
	} catch (InterruptedException e) {
		// Log and Handle exception
		e.printStackTrace();
	}
}
 
开发者ID:ldebello,项目名称:javacuriosities,代码行数:20,代码来源:Step4PhantomReferences.java

示例13: startFinalizer

import java.lang.ref.PhantomReference; //导入依赖的package包/类
public static void startFinalizer(Class<?> paramClass, ReferenceQueue<Object> paramReferenceQueue, PhantomReference<Object> paramPhantomReference)
{
  if (!paramClass.getName().equals("com.google.common.base.FinalizableReference")) {
    throw new IllegalArgumentException("Expected com.google.common.base.FinalizableReference.");
  }
  Thread localThread = new Thread(new Finalizer(paramClass, paramReferenceQueue, paramPhantomReference));
  localThread.setName(Finalizer.class.getName());
  localThread.setDaemon(true);
  try
  {
    if (e != null) {
      e.set(localThread, null);
    }
    localThread.start();
    return;
  }
  catch (Throwable localThrowable)
  {
    for (;;)
    {
      a.log(Level.INFO, "Failed to clear thread local values inherited by reference finalizer thread.", localThrowable);
    }
  }
}
 
开发者ID:ChiangC,项目名称:FMTech,代码行数:25,代码来源:Finalizer.java

示例14: FinalizableReferenceQueue

import java.lang.ref.PhantomReference; //导入依赖的package包/类
/**
 * Constructs a new queue.
 */
@SuppressWarnings("unchecked")
public FinalizableReferenceQueue() {
  // We could start the finalizer lazily, but I'd rather it blow up early.
  queue = new ReferenceQueue<Object>();
  frqRef = new PhantomReference<Object>(this, queue);
  boolean threadStarted = false;
  try {
    startFinalizer.invoke(null, FinalizableReference.class, queue, frqRef);
    threadStarted = true;
  } catch (IllegalAccessException impossible) {
    throw new AssertionError(impossible); // startFinalizer() is public
  } catch (Throwable t) {
    logger.log(Level.INFO, "Failed to start reference finalizer thread."
        + " Reference cleanup will only occur when new references are created.", t);
  }

  this.threadStarted = threadStarted;
}
 
开发者ID:cplutte,项目名称:bts,代码行数:22,代码来源:FinalizableReferenceQueue.java

示例15: allocate

import java.lang.ref.PhantomReference; //导入依赖的package包/类
/**
 * Allocates a byte buffer of the given size.
 * <p>
 * This {@code BufferSource} places no restrictions on the requested size of
 * the buffer.
 */
@Override
public Page allocate(int size, boolean thief, boolean victim, OffHeapStorageArea owner) {
    while (true) {
        processQueue();
        long now = max.get();
        if (now < size) {
          return null;
        } else if (max.compareAndSet(now, now - size)) {
          ByteBuffer buffer;
          try {
            buffer = ByteBuffer.allocateDirect(size);
          } catch (OutOfMemoryError e) {
            return null;
          }
          bufferSizes.put(new PhantomReference<>(buffer, allocatedBuffers), size);
          return new Page(buffer, owner);
        }
    }
}
 
开发者ID:Terracotta-OSS,项目名称:offheap-store,代码行数:26,代码来源:PhantomReferenceLimitedPageSource.java


注:本文中的java.lang.ref.PhantomReference类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。