开发者

Remove blocking from a method

开发者 https://www.devze.com 2023-02-10 03:21 出处:网络
This is homework. I do not want the solution, just a small number of links or ideas. Simply speaking what I want to do is,

This is homework.

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

Simply speaking what I want to do is,

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 be time consuming, which results in my program blocking until results are back. And I want to use different methods of this Object, but program is frozen until doThat() is finished. Those different methods do not necesserely have to be invoked from the method() used in this example, but maybe from outside the object.

I thought about using threads but if I have a huge number of objects (1000+) this probab开发者_StackOverflow中文版ly wont be very efficient (correct me if I am wrong please). I guess if I use threads I have to use one thread per object ?

Is there any other way besides threads that can make the invoking object not block when calling doThat(); ? If threading is the only way, could you provide a link ?

Knowing questions like that get downvoted I will accept any downvotes. But please just a link would be more than great.

Thanks in advance. I hope question is inline with the rules.


I'd also use threads for this, but I simply wanted to add that it would probably be interesting to look at java.util.concurrent.Executors (to create thread pools as you have a number of objects) and the java.util.concurrent.Future and java.util.concurrent.Callable classes which will allow you to launch threads that can return a value.

Take a look at the concurrency tutorial for more info.


I recommend you to create a class that implements Runnable, whose run method does what doThat() does in your sample. Then you can invoke it in a separate Thread in a simple way. Java's Thread class does have a constructor that takes a runnable. Use the run and join methods.

Cheers Matthias


Of course threads are the only solution to handle some jobs in backgrounds, but you are not forced to use a thread just for a single operation to be performed. You can use only one thread that maintains a queue of operations to be performed, in a way that every call to the method doThat adds a new entry into the queue. Maybe some design patterns like "Strategy" can help you to generalize the concept of operation to be performed, in order to store "operation objects" into the thread's queue.


You want to perform several things concurrently, so using threads is indeed the way to go. The Java tutorial concurrency lesson will probably help you.

1000 concurrent threads will impose a heavy memory load, because a certain amount of stack memory is allocated for each thread (2 MB?). If, however, you can somehow make sure there will be only one Thread running at a time, you still can take the thread per object approach. This would require you to manage that doThat() is only called, if the thread produced by a former invocation on another object has already finished.

If you cannot ensure that easily, the other approach would be to construct one worker thread which reads from a double ended queue which object to work on. The doThat() method would then just add this to the end of the queue, from which the worker thread will later extract it. You have to properly synchronize when accessing any data structure from concurrent threads. And the main thread should somehow notify the worker thread of the condition, that it will not add any more objects to the queue, so the worker thread can cleanly terminate.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号