Home  >  Article  >  Java  >  How to combine soft references with queues in Java?

How to combine soft references with queues in Java?

PHPz
PHPzforward
2023-04-25 18:55:081277browse

Explanation

1. Soft references can be used in conjunction with reference queues. If the reference object referenced by the software is garbage collected. If an object has only soft references, the garbage collector will not reclaim it when the storage space is sufficient; if there is insufficient storage space, the storage of these objects will be reclaimed. As long as the garbage collector does not collect, the other party can use the program.

2. The JAVA virtual machine adds the software reference to the reference queue related to it.

Example

    ReferenceQueue<String> referenceQueue = new ReferenceQueue<>();
    String str = new String("abc");
    SoftReference<String> softReference = new SoftReference<>(str, referenceQueue);
 
    str = null;
    // Notify GC
    System.gc();
 
    System.out.println(softReference.get()); // abc
 
    Reference<? extends String> reference = referenceQueue.poll();
    System.out.println(reference); //null

What are the basic data types of java

The basic data types of Java are divided into:

1. Integer type , a data type used to represent integers.

2. Floating point type, a data type used to represent decimals.

3. Character type. The keyword of character type is "char".

4. Boolean type is the basic data type that represents logical values.

The above is the detailed content of How to combine soft references with queues in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete