Is it possible to get a deadlock with ConcurrentHashMap in this circumstance?Does a finally block always get executed in Java?What's the difference between ConcurrentHashMap and Collections.synchronizedMap(Map)?How to get an enum value from a string value in Java?Are there any drawbacks with ConcurrentHashMap?Possible deadlock in C++/boost/threadLock for JavaMEHow to wait for data with ReentrantReadWriteLock?ReentrantReadWriteLock. read and write acquire priorityHow to update 2 objects atomically in a lock-free manner?Documentation contradictions about ReentrantReadWriteLock. Does eventually write lock has priority over read lock or not in fair mode?

Why did the soldiers of the North disobey Jon?

Why was my Canon Speedlite 600EX triggering other flashes?

What is this old US Air Force plane?

Source of the Wildfire?

Is 12 minutes connection in Bristol Temple Meads long enough?

Uh oh, the propeller fell off

Is there any good reason to write "it is easy to see"?

Where to find every-day healthy food near Heathrow Airport?

Why does SSL Labs now consider CBC suites weak?

How do I identify the partitions of my hard drive in order to then shred them all?

Why didn't the Avengers use this object earlier?

Did galley captains put corks in the mouths of slave rowers to keep them quiet?

Why are BJTs common in output stages of power amplifiers?

How to disable Two-factor authentication for Apple ID?

How might a landlocked lake become a complete ecosystem?

How to redirect stdout to a file, and stdout+stderr to another one?

Motorola 6845 and bitwise graphics

How to cope with regret and shame about not fully utilizing opportunities during PhD?

Would life always name the light from their sun "white"

Will the volt, ampere, ohm or other electrical units change on May 20th, 2019?

Offered a new position but unknown about salary?

Why can't I share a one use code with anyone else?

A case where Bishop for knight isn't a good trade

How to make a not so good looking person more appealing?



Is it possible to get a deadlock with ConcurrentHashMap in this circumstance?


Does a finally block always get executed in Java?What's the difference between ConcurrentHashMap and Collections.synchronizedMap(Map)?How to get an enum value from a string value in Java?Are there any drawbacks with ConcurrentHashMap?Possible deadlock in C++/boost/threadLock for JavaMEHow to wait for data with ReentrantReadWriteLock?ReentrantReadWriteLock. read and write acquire priorityHow to update 2 objects atomically in a lock-free manner?Documentation contradictions about ReentrantReadWriteLock. Does eventually write lock has priority over read lock or not in fair mode?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








8















I'm reading the source code of ConcurrentHashMap in JDK8, notice that TreeBin uses a 'read-write' lock to prevent concurrent read and write.



Read threads will go through TreeNodes if there's no concurrent write thread trying to modify the tree structure. When the 'find' operation is done, read thread may:



(1)'CAS' the lockState and 'unpark' the waiter(writer) thread if it exists.



Following is the 'find()' method in source code.



final Node<K,V> find(int h, Object k) 
if (k != null)
for (Node<K,V> e = first; e != null; ) WRITER)) != 0) (ek != null && k.equals(ek))))
return e;
e = e.next;

else if (U.compareAndSwapInt(this, LOCKSTATE, s,
s + READER))
TreeNode<K,V> r, p;
try
p = ((r = root) == null ? null :
r.findTreeNode(h, k, null));
finally WAITER) && (w = waiter) != null)
LockSupport.unpark(w);

return p;



return null;



on the other hand, writer thread may:



  • (2) adding the WAITER state to lockState with 'CAS' operation.


  • (3) set itself to waiter variable.


  • (4) 'park' itself.


here's the writer's code:



 private final void contendedLock() 
boolean waiting = false;
for (int s;;)
if (((s = lockState) & ~WAITER) == 0)
if (U.compareAndSwapInt(this, LOCKSTATE, s, WRITER))
if (waiting)
waiter = null;
return;


else if ((s & WAITER) == 0)
if (U.compareAndSwapInt(this, LOCKSTATE, s, s
else if (waiting)
LockSupport.park(this);




HERE IS MY CONFUSION:



If the four operation above run in this order (2) (1) (3) (4), the operation (1) won't unpark anything because 'waiter' was null at that moment.



Then the waiter will park forever without anyone who can unpark it.



The subsequent writes will be all blocked on the intrinsic lock held by the 'parked' thread.



IS THIS A CHANCE OF DEADLOCK ?



I'm really confused about this. I think perhaps I've missed something in the source code. Need your help if you are familiar with it.










share|improve this question
























  • Also, can please someone explain to me this line in find: if (((s = lockState) & (WAITER|WRITER)) != 0)? Why there is no synchronization here and in its block?

    – dyukha
    Mar 23 at 14:44






  • 1





    @dyukha It means if any waiter or writer here, just go through the tree by 'next' references (like a linked-list). Because every time you add a node into the linked-list, you actually add it at the 'first' position which does not disturb your read threads to go through the other nodes in the list. So no locks needed here

    – Kyne Xiao
    Mar 23 at 15:43

















8















I'm reading the source code of ConcurrentHashMap in JDK8, notice that TreeBin uses a 'read-write' lock to prevent concurrent read and write.



Read threads will go through TreeNodes if there's no concurrent write thread trying to modify the tree structure. When the 'find' operation is done, read thread may:



(1)'CAS' the lockState and 'unpark' the waiter(writer) thread if it exists.



Following is the 'find()' method in source code.



final Node<K,V> find(int h, Object k) 
if (k != null)
for (Node<K,V> e = first; e != null; ) WRITER)) != 0) (ek != null && k.equals(ek))))
return e;
e = e.next;

else if (U.compareAndSwapInt(this, LOCKSTATE, s,
s + READER))
TreeNode<K,V> r, p;
try
p = ((r = root) == null ? null :
r.findTreeNode(h, k, null));
finally WAITER) && (w = waiter) != null)
LockSupport.unpark(w);

return p;



return null;



on the other hand, writer thread may:



  • (2) adding the WAITER state to lockState with 'CAS' operation.


  • (3) set itself to waiter variable.


  • (4) 'park' itself.


here's the writer's code:



 private final void contendedLock() 
boolean waiting = false;
for (int s;;)
if (((s = lockState) & ~WAITER) == 0)
if (U.compareAndSwapInt(this, LOCKSTATE, s, WRITER))
if (waiting)
waiter = null;
return;


else if ((s & WAITER) == 0)
if (U.compareAndSwapInt(this, LOCKSTATE, s, s
else if (waiting)
LockSupport.park(this);




HERE IS MY CONFUSION:



If the four operation above run in this order (2) (1) (3) (4), the operation (1) won't unpark anything because 'waiter' was null at that moment.



Then the waiter will park forever without anyone who can unpark it.



The subsequent writes will be all blocked on the intrinsic lock held by the 'parked' thread.



IS THIS A CHANCE OF DEADLOCK ?



I'm really confused about this. I think perhaps I've missed something in the source code. Need your help if you are familiar with it.










share|improve this question
























  • Also, can please someone explain to me this line in find: if (((s = lockState) & (WAITER|WRITER)) != 0)? Why there is no synchronization here and in its block?

    – dyukha
    Mar 23 at 14:44






  • 1





    @dyukha It means if any waiter or writer here, just go through the tree by 'next' references (like a linked-list). Because every time you add a node into the linked-list, you actually add it at the 'first' position which does not disturb your read threads to go through the other nodes in the list. So no locks needed here

    – Kyne Xiao
    Mar 23 at 15:43













8












8








8


2






I'm reading the source code of ConcurrentHashMap in JDK8, notice that TreeBin uses a 'read-write' lock to prevent concurrent read and write.



Read threads will go through TreeNodes if there's no concurrent write thread trying to modify the tree structure. When the 'find' operation is done, read thread may:



(1)'CAS' the lockState and 'unpark' the waiter(writer) thread if it exists.



Following is the 'find()' method in source code.



final Node<K,V> find(int h, Object k) 
if (k != null)
for (Node<K,V> e = first; e != null; ) WRITER)) != 0) (ek != null && k.equals(ek))))
return e;
e = e.next;

else if (U.compareAndSwapInt(this, LOCKSTATE, s,
s + READER))
TreeNode<K,V> r, p;
try
p = ((r = root) == null ? null :
r.findTreeNode(h, k, null));
finally WAITER) && (w = waiter) != null)
LockSupport.unpark(w);

return p;



return null;



on the other hand, writer thread may:



  • (2) adding the WAITER state to lockState with 'CAS' operation.


  • (3) set itself to waiter variable.


  • (4) 'park' itself.


here's the writer's code:



 private final void contendedLock() 
boolean waiting = false;
for (int s;;)
if (((s = lockState) & ~WAITER) == 0)
if (U.compareAndSwapInt(this, LOCKSTATE, s, WRITER))
if (waiting)
waiter = null;
return;


else if ((s & WAITER) == 0)
if (U.compareAndSwapInt(this, LOCKSTATE, s, s
else if (waiting)
LockSupport.park(this);




HERE IS MY CONFUSION:



If the four operation above run in this order (2) (1) (3) (4), the operation (1) won't unpark anything because 'waiter' was null at that moment.



Then the waiter will park forever without anyone who can unpark it.



The subsequent writes will be all blocked on the intrinsic lock held by the 'parked' thread.



IS THIS A CHANCE OF DEADLOCK ?



I'm really confused about this. I think perhaps I've missed something in the source code. Need your help if you are familiar with it.










share|improve this question
















I'm reading the source code of ConcurrentHashMap in JDK8, notice that TreeBin uses a 'read-write' lock to prevent concurrent read and write.



Read threads will go through TreeNodes if there's no concurrent write thread trying to modify the tree structure. When the 'find' operation is done, read thread may:



(1)'CAS' the lockState and 'unpark' the waiter(writer) thread if it exists.



Following is the 'find()' method in source code.



final Node<K,V> find(int h, Object k) 
if (k != null)
for (Node<K,V> e = first; e != null; ) WRITER)) != 0) (ek != null && k.equals(ek))))
return e;
e = e.next;

else if (U.compareAndSwapInt(this, LOCKSTATE, s,
s + READER))
TreeNode<K,V> r, p;
try
p = ((r = root) == null ? null :
r.findTreeNode(h, k, null));
finally WAITER) && (w = waiter) != null)
LockSupport.unpark(w);

return p;



return null;



on the other hand, writer thread may:



  • (2) adding the WAITER state to lockState with 'CAS' operation.


  • (3) set itself to waiter variable.


  • (4) 'park' itself.


here's the writer's code:



 private final void contendedLock() 
boolean waiting = false;
for (int s;;)
if (((s = lockState) & ~WAITER) == 0)
if (U.compareAndSwapInt(this, LOCKSTATE, s, WRITER))
if (waiting)
waiter = null;
return;


else if ((s & WAITER) == 0)
if (U.compareAndSwapInt(this, LOCKSTATE, s, s
else if (waiting)
LockSupport.park(this);




HERE IS MY CONFUSION:



If the four operation above run in this order (2) (1) (3) (4), the operation (1) won't unpark anything because 'waiter' was null at that moment.



Then the waiter will park forever without anyone who can unpark it.



The subsequent writes will be all blocked on the intrinsic lock held by the 'parked' thread.



IS THIS A CHANCE OF DEADLOCK ?



I'm really confused about this. I think perhaps I've missed something in the source code. Need your help if you are familiar with it.







java deadlock java.util.concurrent concurrenthashmap






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 25 at 2:38







Kyne Xiao

















asked Mar 23 at 14:28









Kyne XiaoKyne Xiao

411




411












  • Also, can please someone explain to me this line in find: if (((s = lockState) & (WAITER|WRITER)) != 0)? Why there is no synchronization here and in its block?

    – dyukha
    Mar 23 at 14:44






  • 1





    @dyukha It means if any waiter or writer here, just go through the tree by 'next' references (like a linked-list). Because every time you add a node into the linked-list, you actually add it at the 'first' position which does not disturb your read threads to go through the other nodes in the list. So no locks needed here

    – Kyne Xiao
    Mar 23 at 15:43

















  • Also, can please someone explain to me this line in find: if (((s = lockState) & (WAITER|WRITER)) != 0)? Why there is no synchronization here and in its block?

    – dyukha
    Mar 23 at 14:44






  • 1





    @dyukha It means if any waiter or writer here, just go through the tree by 'next' references (like a linked-list). Because every time you add a node into the linked-list, you actually add it at the 'first' position which does not disturb your read threads to go through the other nodes in the list. So no locks needed here

    – Kyne Xiao
    Mar 23 at 15:43
















Also, can please someone explain to me this line in find: if (((s = lockState) & (WAITER|WRITER)) != 0)? Why there is no synchronization here and in its block?

– dyukha
Mar 23 at 14:44





Also, can please someone explain to me this line in find: if (((s = lockState) & (WAITER|WRITER)) != 0)? Why there is no synchronization here and in its block?

– dyukha
Mar 23 at 14:44




1




1





@dyukha It means if any waiter or writer here, just go through the tree by 'next' references (like a linked-list). Because every time you add a node into the linked-list, you actually add it at the 'first' position which does not disturb your read threads to go through the other nodes in the list. So no locks needed here

– Kyne Xiao
Mar 23 at 15:43





@dyukha It means if any waiter or writer here, just go through the tree by 'next' references (like a linked-list). Because every time you add a node into the linked-list, you actually add it at the 'first' position which does not disturb your read threads to go through the other nodes in the list. So no locks needed here

– Kyne Xiao
Mar 23 at 15:43












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55314733%2fis-it-possible-to-get-a-deadlock-with-concurrenthashmap-in-this-circumstance%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55314733%2fis-it-possible-to-get-a-deadlock-with-concurrenthashmap-in-this-circumstance%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현