java - 并发ConcurrentHashMap的问题
高洛峰
高洛峰 2017-04-18 10:56:54
0
3
992

这个问题的 说明有点长,

step1 并发map中的put

step2

step3

step4

step5

step6

step7

step8

step9

上面中steop3 表示重复尝试获取锁达到一定的次数之后就 调用lock阻塞当前线程

我想问的问题是关键在 step7 这个if中的tryAcquire 表示的是尝试获取锁,如果获取不成功就返回false,然后将创建一个Node 封装 当前线程添加到一个队列中
(1)其中if的第二个条件表示什么意思? ,好像不仅仅是获取一个节点那么简单的

(2)selfInterrupt 表示自我中断,中断不是说让当前线程放弃正在执行的任务吗(并不是立即放弃,是下次cpu检查的时候查看中断位是吗)

而我的理解是:这里的需求是需要将当前线程阻塞,因为当前线程始终获取不到锁,而这里的实现是使用了中断,中断可以阻塞线程吗?(平时使用中断不都是中断正在等待或者阻塞的线程吗?)

(3)其实最本质想问的是,ReentrantLock实现中 ,当一个线程多次尝试获取不到锁之后是如何阻塞当前线程的? 阻塞之后又是如何唤醒的

求各位开导呐

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(3)
洪涛

Thank you for the invitation, but I really can’t answer because first of all I haven’t studied the source code of ConcurrentHashMap myself, and secondly it is almost impossible to ask such details in the interview. Unless you need to write a class library like guava yourself, I don't think it makes much sense to study these.

左手右手慢动作

Thank you for the invitation, but I'm sorry, my knowledge of concurrency-related learning is still very limited. Let me help you invite people who may be able to answer it

洪涛

I’ll answer as many as I can
Question (1) involves the issue of fair locks. If you can’t get it, insert the Node into the queue. Focus on java.util.concurrent.locks.AbstractQueuedSynchronizerjava.util.concurrent.locks.AbstractQueuedSynchronizer
问题(3)线程将自己挂起,这部分的实现是依靠底层的,参照LockSupportProblem (3) The thread hangs itself. The implementation of this part depends on the underlying layer. Please refer to LockSupport

public static void park(Object blocker) {
    Thread t = Thread.currentThread();
    setBlocker(t, blocker);
    unsafe.park(false, 0L);
    setBlocker(t, null);
}

Wake up using unparkmethod

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template