I'm trying to write small multithread program. Where I want to run same process by multuple threads for different time of frame.
I've created one input file where I have different names to same process and milli seconds the process should run for.
T1|1000
T2|2000
T3|3000
T4|4000
T5|5000
Java Program
package Test;
import java.io.*;
public class MultiProcess {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("/home/maria/Process.txt"));
String st;
while ((st = br.readLine()) != null) {
String[] records = st.split("|");
Thread tr = new ProcessExecution(records[0], Integer.parseInt(records[1]));
tr.start();
}
br.close();
}
static class ProcessExecution extends Thread {
int threadTime = 0;
String processName;
ProcessExecution(String processName, int x) {
this.threadTime = x;
this.processName = processName;
}
@Override
public void run() {
try {
System.out.println("ProcessName: " + this.processName);
Thread.sleep(this.threadTime);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
In above code, ProcessExecution
have different names which are coming from file and want to sleep the current running thread for specific period of time, which is also coming from file.
I'm getting this weird output
ProcessName: T
ProcessName: T
ProcessName: T
ProcessName: T
ProcessName: T
What am dong wrong and how it can be achieved ?
Thanks
Update
All threads are completing at the same time, it seems. It should be staying alive upto the milliseconds coming from file. What's wrong ?