Ich starte jedes mal einen neuen thread oder?
Viele Gruesse
package de.codegym.task.task18.task1823;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
/*
Threads und Bytes
*/
public class Solution {
public static Map<String, Integer> resultMap = new HashMap<String, Integer>();
public static void main(String[] args) throws IOException {
Solution sol = new Solution();
while (true) {
String datei = sol.dateiLesen();
if (datei.equals("exit")) {
break;
} else {
Thread thread = new Thread(new ReadThread(datei));
thread.start();
}
}
}
//
//****************************************************************
// DAtei Namen von der Konsole lesen
//
public String dateiLesen() throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String read = "";
while (true) {
read = reader.readLine();
if (read.equals("exit")) {
break;
} else {
return read;
}
}
return read;
}
public static class ReadThread extends Thread {
private String fileName;
public ReadThread(String fileName) throws IOException {
// Konstruktorrumpf implementieren
this.fileName = fileName;
}
//
//****************************************************************
// Lesen der Datei hier implementieren
//
public void run() {
int[][] array = new int[200][2];
int[] arrayWiederh = new int[200];
int k = 0;
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(fileName);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
int sum = 0;
//
//****************************************************************
// gelesene byte speichern
//
try {
while (inputStream.available() > 0) {
int data = inputStream.read();
array[k][0] = data;
arrayWiederh[k] = -1;
k++;
}
} catch (IOException e) {
e.printStackTrace();
}
//
//****************************************************************
// byte zaehlen
//
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
if (array[i][0] != arrayWiederh[j]) {
if (array[i][0] == array[j][0]) {
array[i][1] = array[i][1] + 1;
arrayWiederh[j] = array[i][0];
}
}
}
}
//
//****************************************************************
// groesste byte finden
//
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
if (array[i][1] > array[j][1]) {
int temp1 = array[i][0];
int temp2 = array[i][1];
array[i][0] = array[j][0];
array[i][1] = array[j][1];
array[j][0] = temp1;
array[j][1] = temp2;
}
}
}
k = 0;
//
//****************************************************************
// byte und Dateiname zur map
//
resultMap.put(fileName, array[0][0]);
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Dateiname: " + fileName + " Anzahl: " + array[0][0]);
}
}
}