Use multithreading to do similar time-consuming processing, such as batch processing, There are times when you want to save time.
For example, to simplify and think Consider processing 10 things that take 5 minutes per process. One thread takes 5 * 10 = 50 minutes, I feel that 10 threads will finish in 5 minutes.
In such a case, writing in thread-safe processing is It's quite a pain. From the next time onwards, in order to reduce the psychological burden in similar situations I wrote it because I wanted to create something like a mold.
For the time being, this is a rough framework that allows you to write thread-safely. I'm going to omit everything except the information I want, so When you embed your own necessary processing, I imagine a "mold" that will be completed.
MultiThreadExecute.java
/**
*Multithreaded class
*Create a thread here and run it
*/
public class MultiThreadExecute {
public static void main(String[] args) {
MultiThreadExecute logic = new MultiThreadExecute();
int status = logic.run();
}
/**Number of threads created*/
private static const THREAD_COUNT = 5;
protected int run() {
//Data acquisition to be processed
int dataSize = ...(abridgement)...;
//Parallel execution processing
//Increase the area by the number of threads to include termination processing
final BlockingQueue<DataDto> taskQueue =
new LinkedBlockingQueue<DataDto>(dataSize + THREAD_COUNT);
//Add to queue
for (DataDto data : (abridgement. Get from somewhere)) {
taskQueue.add(data);
}
final OnExecListener listener = new OnExecListener();
final ExecutorService executor = Executors.newFixedThreadPool(THREAD_COUNT);
for (int i = 0; i < THREAD_COUNT; i++) {
//Make sure you know the end
taskQueue.add(Put an empty DataDto etc.);
//Execution of child thread
final LogicTask task = new LogicTask(taskQueue, listener);
executor.execute(task);
}
//Wait for thread to end
executor.shutdown();
while (!executor.awaitTermination(5, TimeUnit.SECONDS)) {
}
//Get the number of processing etc. as follows
// listener.getTotalCount()
}
}
DataDto.java
public class DataDto {
//Stores arguments to pass for processing
}
Listener.java
/**
*Processing end detection
*/
private static interface Listener {
/**
*Design and create the information you want after processing
*By defining the interface exactly,
*Be flexible in responding to the information you want
*/
void execute();
}
OnExecListener.java
/**
*End of processing listener
*/
private static class OnExecListener implements Listener {
private int totalCount = 0;
@Override
public void execute(){
synchronized (this) {
totalCount++;
}
}
synchronized int getTotalCount() {
return totalCount;
}
}
LogicTask.java
private static class LogicTask implements Runnable {
private final BlockingQueue<DataDto> taskQueue;
private final Listener listener;
/**
*constructor
*/
LogicTask(BlockingQueue<DataDto> taskQueue, Listener listener) {
this.taskQueue = taskQueue;
this.listener = listener;
}
/**
*Executing a task
*/
@Override
public void run() {
try {
while (true) {
if (End judgment) {
break;
}
final DataDto data = taskQueue.take();
//Perform time-consuming processing
exec(data);
}
} catch (//abridgement) {
//abridgement
}
}
private void exec(DataDto data){
//abridgement
return;
}
}
--Be careful about writing multi-thread processing in thread-safe ――By increasing your own "mold", you can't do it only once!
Recommended Posts