Remove lock from method

This is homework.

I do not want a solution, just a small number of links or ideas.

Just saying what I want to do

A simple example:

public class Example { public void method() { int x = doThat(); //Call other methods which do not depend on x return; } } 

doThat() is a method that is known to take a lot of time, which leads to blocking my program until the results return. And I want to use different methods of this object, but the program is frozen until doThat() is finished. These various methods need not be called from the method() used in this example, but possibly from outside the object.

I was thinking about using threads, but if I have a huge number of objects (1000+) it probably won't be very efficient (correct me if I am wrong, please). I think if I use threads, should I use one thread for each object?

Is there any other way besides threads that can cause the caller to not block when doThat(); called doThat(); ? If streaming is the only way, could you provide a link?

Knowing such questions, I get any downvotes. But please, just a link would be more than great.

Thanks in advance. Hope the question complies with the rules.

+7
source share
4 answers

I would also use threads for this, but I just wanted to add that it would be interesting to look at java.util.concurrent.Executors (to create thread pools, since you have several objects) and java.util.concurrent.Future and java.util.concurrent.Callable , which will allow you to run threads that can return a value.

Take a look at the concurrency tutorial for more information.

+3
source

I recommend that you create a class that implements Runnable , the run method does what doThat() does in your example. Then you can call it in a separate topic in a simple way. The Java Thread class has a constructor that runs runnable. Use the run and join methods.

Cheers Matthias

+1
source

Of course, threads are the only solution to handle some jobs in the background, but you shouldn't use a thread to do just one operation. You can use only one thread that maintains a queue of operations to be performed, such that every call to the doThat method adds a new record to the queue. Perhaps some design patterns, such as "Strategy", will help you generalize the concept of the operation being performed in order to save the "operation objects" in the thread queue.

+1
source

You want to do several things at once, so using threads is really the way to go. Java concurrency tutorial will probably help you.

1000 simultaneous threads will place a heavy load on the memory, since a certain amount of stack memory (2 MB?) Is allocated for each thread. If, however, you can somehow make sure that there will be only one thread running at a time, you can still take the thread to the object approach. This would require you to control that doThat() only called if the thread created by the previous call on another object is already completed.

If you cannot easily do this, another approach would be to build one workflow that is read from the double completed queue , which is worth working on. Then the doThat() method will simply add this to the end of the queue, from which the worker thread will subsequently extract it. You must correctly synchronize access to any data structure from parallel threads. And the main thread must somehow notify the worker thread that it will not add more objects to the queue, so the worker thread can end cleanly.

+1
source

All Articles