I have a thread execution queue and you want to view some of its data during its execution in order to track the process.
ThreadPoolExecutorprovides access to its queue, and I can iterate over these objects to call my overridden method toString(), but these are only those threads that are waiting to be executed.
Is there a way to access the threads that are currently running to call my method? Or maybe there is a better approach for this task as a whole?
To clarify a little more about the goal, here is the code for the general idea:
public class GetDataTask implements Runnable {
private String pageNumber;
private int dataBlocksParsed;
private String source;
private String dataType;
public GetDataTask(String source, String dataType) {
this.source = source;
this.dataType = dataType;
}
@Override
public void run() {
}
@Override
public String toString() {
return "GetDataTask{" +
"source=" + source +
", dataType=" + dataType +
", pageNumber=" + pageNumber +
", dataBlocksParsed=" + dataBlocksParsed +
'}';
}
}
and the class containing the performer:
public class DataParseManager {
private static ThreadPoolExecutor executor = new ThreadPoolExecutor(100, 100, 20, TimeUnit.SECONDS, new ArrayBlockingQueue<>(300));
public void addParseDataTask(String source, String dataType) {
executor.execute(new GetDataTask(source, dataType));
}
public String getInfo() {
StringBuilder info = new StringBuilder();
for (Runnable r : executor.getActiveThreads()) {
info.append(((GetDataTask) r).toString()).append('\n');
}
return info.append(executor.toString()).toString();
}
}
source
share