2020-03-06 三七互娛

1.對(duì)Java三大特性的理解?

2.封裝的好處是什么?

3.可以在static環(huán)境中調(diào)用非static變量嗎?

4.HashMap

5.ConcurrentHashMap和synchronizedMap()轉(zhuǎn)換的Map有什么不同?

ConcurrentHashMap是分開(kāi)鎖定每個(gè)鏈表的頭結(jié)點(diǎn),因此允許多個(gè)線(xiàn)程在ConcurrentHashMap中操作不同位置的數(shù)據(jù)。
而synchronizedMap()是鎖定整個(gè)Map集合,同一時(shí)間只允許一個(gè)線(xiàn)程操作整個(gè)Map。

6.為什么線(xiàn)程放回線(xiàn)程池以后不會(huì)被銷(xiāo)毀?

1)因?yàn)榫€(xiàn)程池在判斷任務(wù)是否為空時(shí),執(zhí)行的是一個(gè)永遠(yuǎn)為真的循環(huán),保證線(xiàn)程不斷的運(yùn)行。
runWorker方法中:while (task != null || (task = getTask()) != null) {}

2)線(xiàn)程執(zhí)行完后,會(huì)繼續(xù)執(zhí)行g(shù)etTask獲取任務(wù)的邏輯,
getTask方法中:Runnable r = timed ? workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) : workQueue.take();
timed是判斷當(dāng)前線(xiàn)程是否為非核心線(xiàn)程,如果是核心線(xiàn)程,那么會(huì)調(diào)用 workQueue.take() 一直保持阻塞狀態(tài)直到獲取任務(wù)為止。
如果當(dāng)前線(xiàn)程不是核心線(xiàn)程,那么久會(huì)調(diào)用poll()方法等待一定時(shí)間,如果等待超時(shí),那么就會(huì)回收該線(xiàn)程。

3)通過(guò)這兩個(gè)死循環(huán)來(lái)保證核心線(xiàn)程不會(huì)被銷(xiāo)毀,并且能夠不斷的接收任務(wù)來(lái)執(zhí)行。

有關(guān)方法、注解:
        ThreadPoolExecutor中的runWorker和getTask方法。

        workQueue.take()方法的注解:
        意思是take方法會(huì)在必要時(shí)一直等待直到元素可以用,也就是能夠獲取到元素。
        /**
         * Retrieves and removes the head of this queue, waiting if necessary
         * until an element becomes available.
         * 
         * @return the head of this queue
         * @throws InterruptedException if interrupted while waiting
         */
        E take() throws InterruptedException;

        線(xiàn)程池的底層原理:
        /*
         * Proceed in 3 steps:
         *
         * 1. If fewer than corePoolSize threads are running, try to
         * start a new thread with the given command as its first
         * task.  The call to addWorker atomically checks runState and
         * workerCount, and so prevents false alarms that would add
         * threads when it shouldn't, by returning false.
         *
         * 2. If a task can be successfully queued, then we still need
         * to double-check whether we should have added a thread
         * (because existing ones died since last checking) or that
         * the pool shut down since entry into this method. So we
         * recheck state and if necessary roll back the enqueuing if
         * stopped, or start a new thread if there are none.
         *
         * 3. If we cannot queue task, then we try to add a new
         * thread.  If it fails, we know we are shut down or saturated
         * and so reject the task.
         */

7.像王者榮耀這樣的游戲用到了什么協(xié)議?

udp協(xié)議,及時(shí)更新?tīng)顟B(tài)比保持狀態(tài)信息完整更重要。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容