Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Need solution for multiple threads.

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What should be the solution for the following scenario?



A a = new A();
thread 1 {
a.foo();
a.getMap();
}

thread 2 {
a.foo();
a.getMap();
}

thread 3 {
a.foo();
a.getMap();
}

.....
.....

class A is accessed by multiple threads. When multiple threads operate over the same
instance of class A then I am facing concurrent modification exception which is quite obvious.

Alteratne solution is to use hashtable or some synchronization mechanism but it will be a performace hit because all the other threads have to wait.

More over I want if thread should get the same values that has been set in the map by that thread only.

Is there any alternate approach for the same or any other data structure from java 5 concurrency so that the performance hit is minimal?
 
Sheriff
Posts: 22004
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use ThreadLocal<Map<String,X>>. This allows you to have one map for each thread:
 
Ranch Hand
Posts: 227
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's your requirement here?

If you want the threads to have their own copy of map (and hence no synchronization needed), you can use Rob's method or even simpler - just instantiate class A once per thread.

Since your original code shared the instance of class A among threads, I assume you want all the threads to work on the same map. In this case, how do you want threads to behave at the time of loading of map? Should they all load key-values in the same map simultaneously? If yes, think about colliding keys. If not, it would require a bit of synchronizations. You can't avoid it altogether.
 
john sal
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Aditya : I wanted each thread to have its own copy.

@Rob : Thanks it worked..
 
Rob Spoor
Sheriff
Posts: 22004
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
If you try to please everybody, your progress is limited by the noisiest fool. And this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic