Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Run A Process Synchronously in Java?

I am currently building a small swing app to format drives and change permission and perform a bunch of small other things

Currently, I've come across an issue where running multiple processes will cause them to run asynchronously, which is awesome because it allows for me to dispatch a ton of processes quickly but for what I'm doing I need the process to wait for the one before it to finish.

The problem I've encountered is that the process.waitFor() method delays the GUI from being able to do anything (swing) until all of the processes are finished.

I'm currently using the following code structure (Which I've implemented from this answer) to deploy my commands/processes.

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

class RunSynchronously{
    private static final String sudoPassword = "1234";

    public static void main(String[] args) {
        Process process = null;
        String[] firstCmd = {"/bin/bash", "-c", "echo " + sudoPassword + "| sudo -S chmod 777 -R /media/myuser/mydrive"};
        try {
            process = Runtime.getRuntime().exec(firstCmd);
        } catch (IOException ex) {
            Logger.getLogger(Wizard_Home.class.getName()).log(Level.SEVERE, null, ex);
        }

        try {
            process.waitFor();
        } catch (InterruptedException ex) {
            Logger.getLogger(Wizard_Format.class.getName()).log(Level.SEVERE, null, ex);
        }

        String[] secondCmd = {"/bin/bash", "-c", "echo " + sudoPassword + "| sudo -S chmod 755 -R /media/myuser/mydrive"};
        try {
            process = Runtime.getRuntime().exec(secondCmd);
        } catch (IOException ex) {
            Logger.getLogger(Wizard_Home.class.getName()).log(Level.SEVERE, null, ex);
        }

        try {
            process.waitFor();
        } catch (InterruptedException ex) {
            Logger.getLogger(Wizard_Format.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

How can I delay processes or create a queue while maintaining my GUI as active and not slept/waiting?

like image 989
Tyler Avatar asked Jun 23 '26 08:06

Tyler


2 Answers

How can I delay processes or create a queue while maintaining my GUI as active and not slept/waiting?

Use a SwingWorker so the processes execute in a separate Thread.

Read the section from the Swing tutorial on Concurrency for more information and working examples.

like image 121
camickr Avatar answered Jun 25 '26 22:06

camickr


ExecutorService executor = Executors.newSingleThreadExecutor();
...
executor.execute(() -> yourLongRunningMethod());

The idea is that you create some executor that uses a different thread for execution and execute your heavy task in this thread. In my example, a single-thread executor is used. This will allow you to only pass a minimum amount of time in your GUI thread (task enqueueing is fast).

Please note that using this method you will have at least two threads: a GUI thread and an executor thread. They will probably have to communicate somehow (for example, to display work results in the GUI), so they will probably have to modify some shared data. Here, you may need some synchronization (due to concurrency).

like image 20
Roman Puchkovskiy Avatar answered Jun 25 '26 21:06

Roman Puchkovskiy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!