• Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating HashSet with same name as the Thread  RSS feed

 
Ramakrishna Udupa
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I want to create a HashSet with same instance/name as the Thread which is creating that HashSet. I mean, Suppose T1 is creating a HashSet. I want to give that HashSet instance as T1+"someString".



In the place of instance I want to create the Thread Name. How to do this?

Thanks:
Ramakrishna K.C
 
Marc Cracco
Ranch Hand
Posts: 80
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Someone can correct me if I'm wrong but I would say there is no such thing as dynamic variables in Java. Java variables have to be declared in the source code. While you can do it in things like php java doesn't work that way.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct. There are various other ways one can link a set to a thread, e.g. mapping the thread name to the set, but that seems a strange requirement.
Why would you want to do that.
And, strictly speaking, the set does not have a name. The name is the name of the variable.
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks to me like you want a set per thread. If that's correct, use a ThreadLocal<Set<String>>:
 
Ramakrishna Udupa
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've 1 manager thread for that 5 child threads are there. So totally for all 6 threads I need to create 4 hash-set. 4 hash-set contains different information which is add by parent thread and child threads. Like this, suppose if I've 3 manager threads, Then for each manager threads I need to create 5 threads.
Example:
Ta(manager) child's threads are ta1, ta2, ta3, ta4, ta5. These 6 threads have 4 hash-set(Hash-sets are Ta+"first", Ta+"second", Ta+"third", Ta+"four") which can be modify by Ta and its child threads only.
Tb(manager) child's threads are tb1, tb2, tb3, tb4, tb5. These 6 threads have 4 hash-set(Hash-sets are Tb+"first", Tb+"second", Tb+"third", Tb+"four") which can be modify by Tb and its child threads only.
Tc(manager) child's threads are tc1, tc2, tc3, tc4, tc5. These 6 threads have 4 hash-set(Hash-sets are Tc+"first", Tc+"second", Tc+"third", Tc+"four") which can be modify by Tc and its child threads only.

@Campbell Ritchie

Yes, it seems very strange requirement to me also.


@Rob Spoor

Not per thread. For 6 threads 4 hash-set.


Thanks All:
Ramakrishna K.C
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The naming of variables is an implementation detail - it can't possibly be a requirement. And that's before considering that it isn't possible. One approach would be to use a Map<String, Set<String>>.
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should describe the problem that you are trying to solve and ask for people to suggest the right way of solving it. Currently you have decided on what you think is a correct solution and asking for help with that possibly incorrect solution. What exactly are you trying to achieve?
 
Ramakrishna Udupa
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This is my code. My problem is when (any number of)Threads executes this block, All things are created differently or not? Suppose, When I want to get the Set in the List which is in Map. Simply I can give thread name i.e key and I can retrieve what-ever Set I want in that List of Map. right?

Thanks:
Ramakrishna K.C
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do not mix your solution design with technical implementation details like threads. Use object oriented programming concepts to model your solution with classes that represent your problem domain. Your solution should work whether threading is used or not. You should design it so that after you are done you can just plug an ExecutorService which executes using some pool. There should really not be any need to know which thread is working on which set.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!