快速过一遍Object类源码

在这里插入图片描述
通过阅读Object类的源码,我们发现Object的方法几乎所有方法都是直接或者间接是native方法,即非java代码实现的方法,只有finalize方法是空实现方法,通过阅读finalize方法的注释,可以了解到此方法主要是在gc垃圾回收此对象会回调这个方法

finalize方法

package lang;

public class ObjectTest {

    public static class MyObject {
        @Override
        protected void finalize() throws Throwable {
            System.out.println("gc start...");
            //休眠10秒
            Thread.sleep(10000);
            System.out.println("gc end...");
        }
    }

    public static void main(String[] args) throws InterruptedException {

        MyObject myObject = new MyObject();
        myObject = null;
        System.gc();
        //休眠5秒
        Thread.sleep(5000);
    }
}


我们可以看到当执行gc的时候确实执行了finalize方法,但是当执行gc线程死亡后finalize也就死亡了,并没有继续往下执行,所以在finalize放资源释放代码并不是可靠的,特别释放操作很费时间时

wait和notify

Java多线程实现如果不考虑concurrent包的话,主要就靠synchronized和Object.wait、Object.notify、Object.notifyAll方法了,Object.wait用于让当前线程进入waiting状态也就是休眠状态,会让出CPU时间,而Object.notify和Object.notifyAll就是用于唤醒在Object上休眠的线程,Object.notify是唤醒一个线程,Object.notifyAll是唤醒所有线程。下面就是一个典型的生产者/消费者例子

	public void waitNotifyTest() throws InterruptedException {

        final LinkedList<Integer> queue = new LinkedList();
        final int maxSize = 2;
        final int maxNum = 5;

        Object producerLock = new Object();
        Object consumerLock = new Object();
        Object showLock = new Object();

        Thread producerThread = new Thread() {

            int num = 0;

            @Override
            public void run() {
                while(num <= maxNum) {
                    try {
                        while (queue.size() >= maxSize) {
                            synchronized (producerLock) {
                                System.out.println("仓库已满");
                                producerLock.wait();
                            }
                        }

                        Thread.sleep(1000);
                        synchronized (queue) {
                            queue.add(num++);
                        }
                        System.out.println("生产一个:" + num);
                        synchronized (consumerLock) {
                            consumerLock.notifyAll();
                        }
                        synchronized (showLock) {
                            showLock.notifyAll();
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.err.println("生成线程结束");
            }
        };

        Thread consumerThread = new Thread() {
            @Override
            public void run() {
                int num = 0;
                while (num < maxNum) {
                    try {
                        while (queue.peek() == null && num < maxNum) {
                            synchronized (consumerLock) {
                                consumerLock.wait();
                            }
                        }

                        Thread.sleep(2000);
                        synchronized (queue) {
                            num = queue.poll();
                        }
                        System.out.println("消费一个:" + num);
                        synchronized (producerLock) {
                            producerLock.notifyAll();
                        }
                        synchronized (showLock) {
                            showLock.notifyAll();
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.err.println("消费线程结束");
            }
        };

        Thread showThread = new Thread() {
            @Override
            public void run() {
                while(true) {
                    synchronized (showLock) {
                        try {
                            showLock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                    synchronized (queue) {
                        String str = "库存列表:";
                        for (int i = 0; i < queue.size(); i++) {
                            int num = queue.get(i);
                            str += num;
                            if (i < queue.size() - 1) {
                                str += ",";
                            }
                        }
                        System.out.println(str);
                    }
                }
            }
        };

        producerThread.start();
        consumerThread.start();

        showThread.setDaemon(true);
        showThread.start();

        producerThread.join();
        consumerThread.join();
        System.out.println("主程序结束");
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值