0

I am trying to start N threads from one 'host' thread, and as they execute they have to add elements to the same collection in the original 'host' thread. (Execute a compare method and if it returns true, they add the element they were comparing to the collection).

Now, if I have X 'host' threads, is there any way to differentiate between the X instances of the collection in the host thread, or is the only way to keep a map with X entries mapping the 'host' thread to its respective collection? This seems to be a very back door way.

4
  • Why are different instances associated with each host thread? What is each host thread associated with? (For example, if each host thread is associated with a particular database/connection/client/whatever, then the class for the database/connection/client/whatever should hold the collection.) Commented Jul 2, 2014 at 21:11
  • Can't the host thread tell the other threads it starts, "Here is the particular instance of this collection type that I'm using?" Commented Jul 2, 2014 at 21:21
  • I am trying to compare potentially massive binary trees created at initialization from a SQL query. Idea is create all trees once, so you never have to query the data base again during run time. I figured since each tree has a massive number of nodes, comparing them in parallel would make sense. Now, what if I want to perform multiple tree comparisons in parallel as well and pull all duplicate nodes in respective collections? Its part of my side/pet project. edit: I am basically trying to keep a separate collection associated with each comparison. Commented Jul 2, 2014 at 21:23
  • First of all, post some code to show us what you're talking about. Second of all, a collection can not be in a thread. A collection is in the heap. You may have one designated thread that is responsible for the collection in some way, but that is not a containment relationship. Commented Jul 2, 2014 at 22:03

2 Answers 2

1

It is possible that ThreadLocal might be what you need to allow each of your host threads to have its own version of the collection.

Sign up to request clarification or add additional context in comments.

Comments

0

Have a look at Threads call. It is a way for each host thread to have a unique instance of an object - in this case your collection.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.