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?

What city and town structures are important in a low fantasy medieval world?

How would a physicist explain this starship engine?

Presenting 2 results for one variable using a left brace

US F1 Visa grace period attending a conference

Is there a way to generate a mapping graph like this?

If the Charles SSL Proxy shows me sensitive data, is that data insecure/exposed?

Is there a word for pant sleeves?

Connecting circles clockwise in TikZ

What to call a small, open stone or cement reservoir that supplies fresh water from a spring or other natural source?

Schwa-less Polysyllabic German Noun Stems of Germanic Origin

On a piano, are the effects of holding notes and the sustain pedal the same for a single chord?

Does a windmilling propeller create more drag than a stopped propeller in an engine out scenario?

Best practice for printing and evaluating formulas with the minimal coding

why "American-born", not "America-born"?

How did Jean Parisot de Valette, 49th Grand Master of the Order of Malta, die?

Filter a file list against an integer array?

tikz: 5 squares on a row, roman numbered 1 -> 5

If you attack a Tarrasque while swallowed, what AC do you need to beat to hit it?

Parse a C++14 integer literal

How do you cope with rejection?

Farthing / Riding

Is my company merging branches wrong?

Do seaplanes need to get clearance for takeoff?

Do 'destroy' effects count as damage?



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; )
int s; K ek;
if (((s = lockState) & (WAITER

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; )
int s; K ek;
if (((s = lockState) & (WAITER

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; )
int s; K ek;
if (((s = lockState) & (WAITER

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; )
int s; K ek;
if (((s = lockState) & (WAITER

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권, 지리지 충청도 공주목 은진현