import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.Executor;
import java.util.logging.Level;
import java.util.logging.Logger;
public class metodoLeitura implements Runnable {
long inicio;
public Path path;
public String nome;
public Executor pool;
public metodoLeitura(String p,String n, Executor pool){
inicio = System.currentTimeMillis();
path = Paths.get(p);
nome = n;
pool = pool;
}
public void ler_arquivo() {
try {
byte[] texto = Files.readAllBytes(path);
pool.execute(this);
String lendoArquivo = new String(texto);
System.out.println(nome+"\n"+lendoArquivo+"\n");
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run() {
ler_arquivo();
long fim = System.currentTimeMillis();
long total = (fim - inicio);
System.out.println("Tempo Total: "+total);
}
}
Galera como eu posso poderia usar o método executorThreads para realizar a leitura de arquivos ? Estou com dúvida de como implementar, abaixo fiz a classe de leitura mas não conseguir executala usando um pool de threads
1 Resposta
E aqui a classe principal
import java.util.*;
public class metodoleituratest
{
public static void main(String[] args)
{
Thread threadTeste1 = new Thread(new metodoLeitura("arquivo2.txt","1#"));
Thread threadTeste2 = new Thread(new metodoLeitura("arquivo3.txt","2#"));
Thread threadTeste3 = new Thread(new metodoLeitura("arquivo1.txt","3#"));
Thread threadTeste4 = new Thread(new metodoLeitura("arquivo4.txt","3#"));
threadTeste1.start();
threadTeste2.start();
threadTeste3.start();
threadTeste4.start();
}
}
Criado 10 de abril de 2017
Ultima resposta 10 de abr. de 2017
Respostas 1
Participantes 1
Alura O que é Python? — um guia completo para iniciar nessa linguagem de programação Acesse agora o guia sobre Python e inicie sua jornada nessa linguagem de programação: o que é e para que serve, sua sintaxe e como iniciar nela!
Casa do Codigo Back-end Java: Microsservicos, Spring Boot e Kubernetes Por Eduardo Felipe Zambom Santana — Casa do Codigo