patternjavaMinor
Reading output from multiple threads and writing to a file
Viewed 0 times
readingfilethreadswritingoutputmultipleandfrom
Problem
There are 10 threads and I have 15 tasks. Now I have submitted all these tasks to these threads. I need to write all these threads output to a file which I was not successful.
I am getting output by running all the threads.
ThreadPool.java (Creates a Thread pool and adds all the tasks to the Blocking queue and submits)
```
public class ThreadPool {
ExecutorService execService = null;
BlockingQueue> tasks = null;
BlockingQueue queue = null;
// Get a list of Employee ID's
public ThreadPool(List empIDList) {
try {
tasks = new ArrayBlockingQueue>(empIDList.size());
for(String empNum : empIDList) {
tasks.add(new ThreadTask(empNum));
}
performExecution(tasks);
} catch(Exception e) {
e.printStackTrace();
}
}
private void performExecution(BlockingQueue> tasks) {
try {
execService = Executors.newFixedThreadPool(10);
queue = new LinkedBlockingQueue();
Runnable reader = new ReaderEmp(queue);
Thread readerThread = new Thread(reader);
readerThread.start();
for(Callable call : tasks) {
queue.add(executeTask(call, 5));
}
execService.shutdown();
} catch(Exception e) {
e.printStackTrace();
}
}
private String executeTask(Callable task, int seconds) {
try {
String[] future = execService.submit(task).get(seconds, TimeUnit.SECONDS);
System.out.println("Successfully Executed for " + future[0]);
return future[0];
} catch(TimeoutException toe) {
System.out.println("Time Out for " + ((ThreadTask) task).getEmpID());
} catch (ExecutionException ee) {
System.out.println("Execution Exception for " + ((ThreadTask) task).getEmpID());
} catch (InterruptedException ie) {
System.out.println("Interrupte
I am getting output by running all the threads.
ThreadPool.java (Creates a Thread pool and adds all the tasks to the Blocking queue and submits)
```
public class ThreadPool {
ExecutorService execService = null;
BlockingQueue> tasks = null;
BlockingQueue queue = null;
// Get a list of Employee ID's
public ThreadPool(List empIDList) {
try {
tasks = new ArrayBlockingQueue>(empIDList.size());
for(String empNum : empIDList) {
tasks.add(new ThreadTask(empNum));
}
performExecution(tasks);
} catch(Exception e) {
e.printStackTrace();
}
}
private void performExecution(BlockingQueue> tasks) {
try {
execService = Executors.newFixedThreadPool(10);
queue = new LinkedBlockingQueue();
Runnable reader = new ReaderEmp(queue);
Thread readerThread = new Thread(reader);
readerThread.start();
for(Callable call : tasks) {
queue.add(executeTask(call, 5));
}
execService.shutdown();
} catch(Exception e) {
e.printStackTrace();
}
}
private String executeTask(Callable task, int seconds) {
try {
String[] future = execService.submit(task).get(seconds, TimeUnit.SECONDS);
System.out.println("Successfully Executed for " + future[0]);
return future[0];
} catch(TimeoutException toe) {
System.out.println("Time Out for " + ((ThreadTask) task).getEmpID());
} catch (ExecutionException ee) {
System.out.println("Execution Exception for " + ((ThreadTask) task).getEmpID());
} catch (InterruptedException ie) {
System.out.println("Interrupte
Solution
Quite a few things are wrong with your code.
- Several threads are trying to write to the same file simultaneously. Either synchronize on a shared FileWriter or simply have each task write to its own file, and merge them afterwards. Given that the computation itself is trivial, the latter is probably preferable. and if the size of the input is small, it's probably not even worth doing this over multiple threads.
- You mix the use of an ExecutorService, with managing threads yourself. Define all the things you want to do on different threads as
RunnableorCallableimplementations, and submit those to an ExecutorService.
- You do not properly handle InterruptedException.
- tasks is a
LinkedBlockingQueue, but could simply be anArrayList.
Context
StackExchange Code Review Q#27201, answer score: 2
Revisions (0)
No revisions yet.