É possível pegar os atributos de um objeto Thread? pois estou utilizado threads para ler varios arquivos(um por thread) (que possui numeros float separados por \n) armazenar os floats fazer a soma dos floats e fazer a media de valores, porem é necessário fazer uma soma geral dos arquivos porem não consigo pegar os valores gerados dentro da thread pela classe principal.
classe principal:
package multithread;
import java.util.ArrayList;
import java.util.List;
public class MultiThread {
public static void main(String[] args) {
List<String> pathsCemMil = new ArrayList<String>();
pathsCemMil.add("C:\\Users\\Ricardo\\Documents\\NetBeansProjects\\MultiThread\\src\\arquivos\\NumerosFloatCemMil_A.txt");
pathsCemMil.add("C:\\Users\\Ricardo\\Documents\\NetBeansProjects\\MultiThread\\src\\arquivos\\NumerosFloatCemMil_B.txt");
pathsCemMil.add("C:\\Users\\Ricardo\\Documents\\NetBeansProjects\\MultiThread\\src\\arquivos\\NumerosFloatCemMil_C.txt");
List<String> pathsDezMil = new ArrayList<String>();
pathsDezMil.add("C:\\Users\\Ricardo\\Documents\\NetBeansProjects\\MultiThread\\src\\arquivos\\NumerosFloatDezMil_A.txt");
pathsDezMil.add("C:\\Users\\Ricardo\\Documents\\NetBeansProjects\\MultiThread\\src\\arquivos\\NumerosFloatDezMil_B.txt");
pathsDezMil.add("C:\\Users\\Ricardo\\Documents\\NetBeansProjects\\MultiThread\\src\\arquivos\\NumerosFloatDezMil_C.txt");
List<String> pathsUmMilhao = new ArrayList<String>();
pathsUmMilhao.add("C:\\Users\\Ricardo\\Documents\\NetBeansProjects\\MultiThread\\src\\arquivos\\NumerosFloatUmMilhao_A.txt");
pathsUmMilhao.add("C:\\Users\\Ricardo\\Documents\\NetBeansProjects\\MultiThread\\src\\arquivos\\NumerosFloatUmMilhao_B.txt");
pathsUmMilhao.add("C:\\Users\\Ricardo\\Documents\\NetBeansProjects\\MultiThread\\src\\arquivos\\NumerosFloatUmMilhao_C.txt");
List<Thread> threadsDezMil = new ArrayList<Thread>();
for(String path : pathsDezMil){
threadsDezMil.add(new Worker(path));
}
List<Thread> threadsCemMil = new ArrayList<Thread>();
for(String path : pathsCemMil){
new Worker(path).start();
}
List<Thread> threadsUmMilhao = new ArrayList<Thread>();
for(String path : pathsUmMilhao){
new Worker(path).start();
}
}
}
Classe thread:
package multithread;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Worker extends Thread{
String path;
double valoresSoma;
double valoresMedia;
int valoresAcimaMedia;
public Worker(String path){
this.path = path;
}
public void run(){
try {
List<Double> valores = new ArrayList<Double>();
valores = reader(this.path);
valoresSoma = soma(valores);
valoresMedia = media(valoresSoma,valores.size());
valoresAcimaMedia = acimaMedia(valores, valoresMedia);
System.out.println(this.path);
System.out.println(valores.size());
System.out.println(valoresSoma);
System.out.println(valoresMedia);
System.out.println(valoresAcimaMedia);
} catch (IOException ex) {
Logger.getLogger(Worker.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static List<Double> reader(String path) throws IOException{
List<Double> valores = new ArrayList<>();
FileReader fileReader = new FileReader(path);
BufferedReader reader = new BufferedReader(fileReader);
String data = null;
while((data = reader.readLine()) != null){
valores.add(Double.parseDouble(data));
}
fileReader.close();
reader.close();
return valores;
}
public static double soma(List<Double> valores){
double soma = 0;
for(Double valor : valores){
soma += valor;
}
return soma;
}
public static double media(double soma, int qtde){
return soma/qtde;
}
public static int acimaMedia(List<Double> valores, double media){
int qtde = 0;
for(Double valor : valores){
if(valor > media){
qtde++;
}
}
return qtde;
}
}
Exemplo de arquivo que possui os valores float:
898.29
653.46
572.77
669.53
695.89
400.91
392.73
547.55
748.77
38.43