Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

383
Visualizações
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?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

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.

over 4 years ago · Santiago Trujillo Relatório

0

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).

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda