JAVA语言之Java基础学习之--理解Object类
小标 2019-05-09 来源 : 阅读 1056 评论 0

摘要:本文主要向大家介绍了JAVA语言之Java基础学习之--理解Object类,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。

本文主要向大家介绍了JAVA语言之Java基础学习之--理解Object类,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。


看Java API的Object类, 一共11个方法。按使用的频度排名:




  1. toString() 这个方法最常用在打日志,定位代码问题。



  2. equals()hashCode(), 这两个方法的使用经典例子是HashMap的源码



    public V put(K key, V value) {
        if (table == EMPTY_TABLE) {
            inflateTable(threshold);
        }
        if (key == null)
            return putForNullKey(value);
        int hash = hash(key);
        int i = indexFor(hash, table.length);
        for (Entry<K,V> e = table[i]; e != null; e = e.next) {
            Object k;
            if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
                V oldValue = e.value;
                e.value = value;
                e.recordAccess(this);
                return oldValue;
            }
        }

        modCount++;
        addEntry(hash, key, value, i);
        return null;
    }


equals() 比 hashCode() 要常用一点。



  1. wait() 和 notify() 这个开发很少会直接用到,但是间接用到的场景不少,属于偏内功的点。wait/notify属于Object类最难理解的点了,因为它的基石是多线程。学习思路还是三步走。



step 1: 看文档说明


wait()
Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.

notify()
Wakes up a single thread that is waiting on this object's monitor.


这个文档说明,看完基本一头雾水,两个方法都提到了如下的核心概念thread, object's monitor。 先把这些概念放一边,看看是怎么用的。


step 2: 运行Demo


给个demo辅助理解,一个线程是干活的,另一个线程是管事的。管事的等活干完验收。


public class WaitNotifyDemo {

    static class Worker extends Thread{

        private volatile int status=0; // 1: 完成, -1: 出错

        @Override
        public void run(){

            try {
                System.out.println(Thread.currentThread().getName() +": 开始干活");
                Thread.sleep(3000);
                System.out.println(Thread.currentThread().getName() +": 完成任务");

                status = 1;
                // 通知主线程
                synchronized (this){
                    this.notify();
                }

            } catch (InterruptedException e) {
                e.printStackTrace();
                status = -1;
            }
        }

        public int getStatus(){
            return status;
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Worker worker= new Worker();
        worker.start();

        synchronized (worker){
            int status;
            while ((status=worker.getStatus())!=1){
                worker.wait();
                if(status==-1)
                    throw new RuntimeException("出错了");
            }
        }
        System.out.println("任务已经完成");
    }
}


step3: 折腾demo
接下来, 我试了一下, 把notify的代码去掉,也能正常运行。 这就让人困惑了,文档明明说必须调用notify,wait才能结束。接下来再看文档:


A thread can also wake up without being notified, interrupted, or timing out, a so-called spurious wakeup


所以, wait方法必须在while循环中调用。好,解答了一点疑惑。但是每次Worker线程结束时没有调用notify,主线程就能正常退出, 这个也说不通。 唯一的解释是: JVM内部在某个地方调用了notify。看openjdk的源码, 果然如此:从start0开始, 定位到线程在退出时会调用lock.notify_all(thread);。只是这里的代码是JVM内部的代码,比较底层。 


其实,这个例子更简洁的写法是worker.join()。 如果看Thread.join()的源码,发现它的实现恰好就是调用了wait。




  1. clone() 这个需要理解深度克隆, 知识点不复杂。




  2. getclass() 这个放在后面,但是用得还挺多的,特别是写框架。



  3. finalize() 这个已经不他推荐使用了, 尽量不干扰GC的节奏。



总结一下,Object类是Java的基石。这里比较难一点的就是wait/notify.  学习Java的API, 如果想理解透彻一点,估计绕不开JVM的c++源码。

   

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注编程语言JAVA频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved