0

I am working on designing a multi-user application and would like to have a singleton service class that is shared amongst all of the user threads.

Within this class, I would like to have a method that does some heavy lifting to eventually compile a seperate list for each user, I am under the impression that since this is a method, each user thread will recieve its own sperate copy of this on its call stack.

Since this process is time consuming, I would like create multiple threads within the method to handle the processing and add items to a single list that was declared and instantiated within the method.

In the end I am envisioning a list complete list for each user that will then be able to be used throughout the rest of the application within the users thread.


My question is: How do accomplish sharing the list declared in the method amongst the multiple threads I want to create within the method while maintaining thread safety?

The flow chart below may explain everything better

enter image description here

oznomal
  • 439
  • 2
  • 8
  • 22
  • Why not use a `Future`, implement the task, and avoid the issues of the Singleton? – KevinO Nov 15 '18 at 02:02
  • Local variables are only visible to one thread at a time, so they're normally used to isolate data that isn't shared between threads. To publish a local variable where other threads can see it, use Safe Publication: https://stackoverflow.com/questions/801993/java-multi-threading-safe-publication – markspace Nov 15 '18 at 02:34
  • Don't get confused though. If a local variable is a method parameter and that value was copied from some part of the code that is already public and visible to other threads, then (obviously) that data is shared. Only data/objects *created* locally is not shared. – markspace Nov 15 '18 at 02:37
  • Since the list represents something, I'd suggest making a class for that purpose. The list would then be defined as a thread safe instance variable. https://stackoverflow.com/questions/8203864/choosing-the-best-concurrency-list-in-java – robertf Nov 15 '18 at 02:50
  • Well, we know next to nothing about the OP's actual problem, so I think it's a little premature to suggest implementations. I was going to suggest that the list be made *not* an instance variable, that it be kept local, and simple pass a reference to the specific threads that need to know about it. But I really don't know, because the OP's problem is so vague and general. The whole thing could be IO bound for example, and then threads don't help. – markspace Nov 15 '18 at 02:56
  • I hear what you're saying, but also recognize the OP is simply looking for a solution to the question. I don't really buy into the stateless singleton service class and want the OP think about what they're doing. Since the method will be doing some "heavy processing" and involve the creation of multiple threads, I would expect it to have a decent amount of code. Thus, I'd prefer to have a class to represent it, rather than some indiscriminate list being passed around. – robertf Nov 15 '18 at 03:21
  • In terms of end goals, the method will be scraping data from a website containing anywhere from 1-40 pages of table data. Info from each row will be collected and stored within the collection (a queue) where it will be processed later on by the application. The actual code to scrape the data is rather brief, but the process of scraping each page 1 by 1 is a bit slower than I’d like. This is where I’d like the multi-threading to come into to play to help gather that data a bit faster. I like the idea of passing the the ref to the queue and don’t really think I need a separate class right now. – oznomal Nov 15 '18 at 04:48

0 Answers0