Thread que não para

5 respostas
pix

Quero fazer um Thread que execute a “todo momento”, e verificar se existe arquivos em determinada pasta, se não existir ela “dorme” novamente, o problema que ela executa uma única vez e para o processo, a idéia é que o processo pare só com uma interrupção “humana”.
segue o código

public void run() { String dir = "C:\\java\\NFE\\xml"; File diretorio = new File(dir); File fList[] = diretorio.listFiles(); System.out.println("Numero de arquivos no diretorio : " + fList.length); if (fList.length < 1) { NotaFiscal1 nota = new NotaFiscal1(); try { nota.geraNota(); } catch (FileNotFoundException ex) { Logger.getLogger(SimpleThread.class.getName()).log(Level.SEVERE, null, ex); } } else { try { sleep(1); } catch (InterruptedException ex) { Logger.getLogger(SimpleThread.class.getName()).log(Level.SEVERE, null, ex); } } for (int i = 0; i < fList.length; i++) { System.out.println(fList[i].getName()); }

5 Respostas

mduques
public void run() {
        String dir = "C:\\java\\NFE\\xml";

   while(true){
        File diretorio = new File(dir);
        File fList[] = diretorio.listFiles();
        System.out.println("Numero de arquivos no diretorio : " + fList.length);
        if (fList.length < 1) {
            NotaFiscal1 nota = new NotaFiscal1();
            try {
                nota.geraNota();
            } catch (FileNotFoundException ex) {
                Logger.getLogger(SimpleThread.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            try {
                sleep(1);
            } catch (InterruptedException ex) {
                Logger.getLogger(SimpleThread.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        for (int i = 0; i < fList.length; i++) {
            System.out.println(fList[i].getName());
        }
  }

}
sergiotaborda

além do loop infinito é necessário chamar Sleep. quando fizer isso o java irá forçá-lo a tratar interruptedException.
A forma correta de tratar isso é fazer break do while.

ViniGodoy

Ou seja, assim:

public void run() { String dir = &quot;C:\\java\\NFE\\xml&quot;; try { while(true){ File diretorio = new File(dir); File fList[] = diretorio.listFiles(); System.out.println(&quot;Numero de arquivos no diretorio : &quot; + fList.length); if (fList.length &lt; 1) { NotaFiscal1 nota = new NotaFiscal1(); try { nota.geraNota(); } catch (FileNotFoundException ex) { Logger.getLogger(SimpleThread.class.getName()).log(Level.SEVERE, null, ex); } } else { sleep(100); } for (int i = 0; i &lt; fList.length; i++) { System.out.println(fList[i].getName()); } } } catch (InterruptedException ex) { Logger.getLogger(SimpleThread.class.getName()).log(Level.FINER, null, ex); } }

pix

Show de bola, funcionou perfeitamente!
:slight_smile:

ViniGodoy

Outra coisa.

Ao disparar essa thread, não esqueça de definir a propriedade daemon como true:

Thread verificadorDeArquivos = new Thread(verificadorDeArquivosRunnable); verificadorDeArquivos.setDaemon(true);

Isso garante que o seu programa finalizará, caso só essa thread sobre rodando.

Criado 25 de novembro de 2008
Ultima resposta 25 de nov. de 2008
Respostas 5
Participantes 4