What is the correct way to do long-term work outside of EDT?

In a Java 1.5 desktop application (it should work on many MacOS X machines that nerver see 1.6 VMs due to Apple policy), what is the right way to do lengthy calculations outside of EDT?

Say, for example, when a user presses a button that starts an operation: I get an EDT notification, and I want to run some method (say crunchData()).

Here is one way to do this:

        final Thread t = new Thread( new Runnable() {
            public void run() {
                crunchData();
            }
        } );
        t.start;

I mean: this does what I want, but every time a user runs a potentially long-term task, I use the above idiom. And I feel that I always unnecessarily create many tasks (moreover, although sometimes the operation can be lengthy, sometimes it is not, and in this case I would like the application to be as responsible as possible).

Another way to do this would be to have a different (non-EDT) thread (or thread pool) that always works, say waiting in a lock queue and executing, say Runnable, that I have to queue where I need to perform a long operation.

What is the right way to handle this?

EDIT: - SwingWorker? ( ) , SwingWorker ?

+3
1
+3

Source: https://habr.com/ru/post/1760121/


All Articles