• Post Reply Bookmark Topic Watch Topic
  • New Topic

What threading primitives can solve this problem ?

 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have a lazily loaded graph. Basically the nodes of the graph can be expanded/collapsed at will by the user.

Each node of the graph has a unique uri.

I want to implement a check within the method expandNodeWithUri such that if there was a previous call to "expandNodeWithUri"; then subsequent calls will not regenerate duplicate nodes.

What threading construct is suitable to solve this problem ?
Thanks

Pho
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pho Tek:

I want to implement a check within the method expandNodeWithUri such that if there was a previous call to "expandNodeWithUri"; then subsequent calls will not regenerate duplicate nodes.


Let know if i have got it rite, You want to:
  • Let only one thread generate the nodes at any time
  • Once generated you do not want to generate it again


  • If the above is what you intend, then,
    First thing you need is to use a cache for the results, i.e., you store the results in memory against a key that uniquely identifies the result (I think, it will be URI in your case.)
    In order to ensure that only one thread process a URI at a time, you can do the following:
  • Make the entire method synchronized. (This will lead to a lot of lock contention if there are a lot of concurrent requests. So its not really a good approach if the concurrent requests are for different URIs.)
  • Synchronize on the URI object/string. (If the uri object is the same for same URIs, else you can use the interned uri string as the monitor)

  •  
    Pho Tek
    Ranch Hand
    Posts: 782
    Chrome Python Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes you comprehended my scenario correctly.

    Your second solution interests me more since I don't want the contention to slow down threads.

    Thanks

    P
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!