`

jdk6.0从入门到精通-----chapter2--参数传递,强弱引用

JVM 
阅读更多
...用于不定参数

package uncertainParameter;

public class Testparameter {
public void argtest(Object ...objects ){//...表示不定参数个数
	for(int i=0;i<objects.length;i++){
		System.out.println(objects[i]);
	}
}
	public static void main(String[] args) {

	new Testparameter().argtest(23,"hello",true);
	
	}

}


java的引用例子,强引用,弱引用,虚引用

package reference;
import java.lang.ref.SoftReference; 
import java.lang.ref.WeakReference;
import java.lang.ref.PhantomReference;//虚引用
import java.lang.ref.ReferenceQueue; 
import java.util.Set;
import java.util.HashSet;

public class TestReferences 
{ 
    public static void main(String[] args) 
    {
        int length=3;
        
        //创建length个MyObject对象的强引用
        Set<MyObject> a = new HashSet<MyObject>();
        for(int i = 0; i < length; i++) 
        {
            MyObject ref=new MyObject("Hard_" + i);
            System.out.println("创建强引用:" +ref);
            a.add(ref);
        }
        a=null; //强引用对象,除非显示调用对象=null,否则不会立刻回收空间
        System.gc();
        
        //创建length个MyObject对象的软引用
        Set<SoftReference<MyObject>> sa = new HashSet<SoftReference<MyObject>>();
        for(int i = 0; i < length; i++) 
        {
            SoftReference<MyObject> ref=new SoftReference<MyObject>(new MyObject("Soft_" + i));
            System.out.println("创建软引用:" +ref.get());
            sa.add(ref);
        }
        System.gc(); // 空间足够,不回首,空间不足,回收对象

        //创建length个MyObject对象的弱引用
        Set<WeakReference<MyObject>> wa = new HashSet<WeakReference<MyObject>>();
        for(int i = 0; i < length; i++) 
        {
            WeakReference<MyObject> ref=new WeakReference<MyObject>(new MyObject("Weak_" + i));
            System.out.println("创建弱引用:" +ref.get());
            wa.add(ref); 
        }
        System.gc();  //一旦发现被弱引用的对象,立即回收,不管空间足够与否

        //创建length个MyObject对象的虚引用
        ReferenceQueue<MyObject> rq = new ReferenceQueue<MyObject>();
        Set<PhantomReference<MyObject>> pa = new HashSet<PhantomReference<MyObject>>();
        for(int i = 0; i < length; i++) 
        {
            PhantomReference<MyObject> ref = new PhantomReference<MyObject>(new MyObject("Phantom_" + i), rq);
            System.out.println("创建虚引用:" +ref.get());
            pa.add(ref);
        }
        System.gc();
    }
}

class MyObject
{
    private String id;

    public MyObject(String id) 
    { 
        this.id = id; 
    }

    public String toString() 
    { 
        return id; 
    }
//    JVM保证在一个对象所占用的内存被回收之前,如果它实现了finalize方法,则该方法一定会被调用
    public void finalize() 
    {
        System.out.println("回收对象:" + id);
    }
}


执行结果:
创建强引用:Hard_0
创建强引用:Hard_1
创建强引用:Hard_2
回收对象:Hard_2
回收对象:Hard_1
回收对象:Hard_0
创建软引用:Soft_0
创建软引用:Soft_1
创建软引用:Soft_2
创建弱引用:Weak_0
创建弱引用:Weak_1
创建弱引用:Weak_2
回收对象:Weak_2
回收对象:Weak_1
回收对象:Weak_0
创建虚引用:null
创建虚引用:null
创建虚引用:null
回收对象:Phantom_2
回收对象:Phantom_1
回收对象:Phantom_0


finalize的用法

package reference;

class A {
	B b;

	public void finalize() {
		System.out.println("method A.finalize at " + System.nanoTime());
	}
}

class B {
	public void finalize() {
		System.out.println("method B.finalize at " + System.nanoTime());
	}
}

public class Test {

	public static void main(String[] args) {
		A a = new A();
		a.b = new B();
		a = null;
		System.gc();
	}
}

method B.finalize at 12487493406564
method A.finalize at 12487493840976




分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics