开发者

Java: Calling self-invoking method without freezing?

开发者 https://www.devze.com 2023-04-06 16:13 出处:网络
So this is really complicated, it took me a while to realize what\'s actually happening. Hopefully you understand it better than me though.

So this is really complicated, it took me a while to realize what's actually happening. Hopefully you understand it better than me though.

I have a Swing class that displays the GUI. In the GUI i have a button, and in the Swing class i have a method that is called whenever i click the button.

When that method is called, i call another method in an object called "Manager". The method in manager then calls another method in a class called "Core". That method in Core sets a local variable,开发者_Python百科 and then calls another method in Core, that self-invokes itself.

The problem is that since it's self-invocing, it never stops running, right? And since it never stops running, nothing is ever returned to the first method in Core. And since nothing is returned to that method, nothing is returned to Manager either. And since that method is never called, the GUI class never gets a response, which leaves the GUI frozen.

Horribly sorry for the messy description. I can't post the code unfortunately. I hope anyone gets my point though, someone must have had the same issue before.

Thanks!

EDIT:

I forgot to mention that the Core class is a thread.


Your long-running process is preventing the main Swing thread, the EDT or event dispatch thread, from continuing, and this will make your GUI completely unresponsive. The solution is to do any long-running process in a background thread such as created by a SwingWorker object. Please check out the link called Concurrency in Swing to learn more about use of SwingWorker objects.


Self-invocation is called recursion. It's a powerful technique, but could lead to infinite loops (hanging) if you're not careful. You need to make sure every recursion (i.e. every time the method invokes itself) something changes towards a terminating state. For example, you could have a number that is guaranteed to decrease every recursion and have as exit condition that your number is negative. Another example is that you're recursively "eating up" some data structure, let's say a string, until there's nothing left. Usually this "something that changes towards a terminating state" is passed to the method as an argument. Your recursive method should start with a check: is my argument in a terminating state? If yes, terminate, if no, do magic.

Secondly, with Swing you should be careful not to violate it's architecture. It's not really MVC, but rather a 2-layered framework. If you're interested in how to use Swing, I recommend reading up on design patterns like MVC.


You got a number of problems.

1) Your recursive function needs an exit condition. So something like

public int imRecursive(int arg) {
   if (arg > 100) return;
   imRecursive(arg++);    
}

in that example, imRecursive doesn't get called over and over, it stops once arg reaches 100.

2) With a swing app, only GUI related code should run in the main event-dispatching thread. If your recursive method is long running, you should use a SwingWorker to do it in another thread so your GUI doesn't lock up.


Post some code. You say Core is a Thread, I assume that means class Core extends Thread but we need to see where you are spawning a new thread (calling a method in a class that extends Thread does not make it run in a separate thread).

Your recursive (self-invoking) method, if it never breaks the recursion, will before too long cause StackOverflowError. If you are not getting that then either you are not using a recursive function or breaking the recursion somewhere. The right way to code a method that never terminates is not recursion, it is iteration.

The GUI freezing is almost certainly because some time-consuming processing is occurring in the GUI thread, one more reason to believe that the method in Core is not running in a separate thread.


I don't understand WHY you have to self-invoke your method...

are you trying to do some recursion?

if the method never ends, you got a infinite loop, its not good.

Depending for what you are trying, you may use Java Threads, or, rethink your code.. i bet you're doing something wrong.


You need to analyse your self-invoking method call - This is a recursive call - but every recursive method must have some condition that stops the recursion - check under what conditions in your case you should get such a condition

0

精彩评论

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