Multithreaded to fit in [Java] template

Introduction

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.

Implementation example

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;
	}
}

What I thought

--Be careful about writing multi-thread processing in thread-safe ――By increasing your own "mold", you can't do it only once!

Recommended Posts

Multithreaded to fit in [Java] template
How to learn JAVA in 7 days
Log output to file in Java
How to use classes in Java?
How to name variables in Java
Try to implement Yubaba in Java
How to concatenate strings in java
How to implement date calculation in Java
How to implement Kalman filter in Java
Multilingual Locale in Java How to use Locale
Try to solve Project Euler in Java
Easy to make Slack Bot in Java
Try to implement n-ary addition in Java
How to do base conversion in Java
Change List <Optional <T >> to Optional <List <T >> in Java
Convert SVG files to PNG files in Java
How to implement coding conventions in Java
How to embed Janus Graph in Java
How to get the date in java
Add footnotes to Word documents in Java
[Java Bronze] 5 problems to keep in mind
Sample to unzip gz file in Java
Java to C and C to Java in Android Studio
Add SameSite attribute to cookie in Java
Partization in Java
Changes in Java 11
Rock-paper-scissors in Java
[Java] .gitignore template
[Java] Introduction to Java
Introduction to java
Pi in Java
FizzBuzz in Java
Two ways to start a thread in Java + @
I want to send an email in Java.
How to display a web page in Java
CORBA seems to be removed in Java SE 11. .. ..
Code to escape a JSON string in Java
Try to create a bulletin board in Java
I tried to implement deep learning in Java
How to get Class from Element in Java
There seems to be no else-if in java
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
rsync4j --I want to touch rsync in Java.
How to hide null fields in response in Java
Library "OSHI" to get system information in Java
I tried to output multiplication table in Java
[Java] How to substitute Model Mapper in Jackson
How to solve an Expression Problem in Java
I tried to create Alexa skill in Java
How to write Java String # getBytes in Kotlin?
Things to watch out for in Java equals
[java] sort in list
Read JSON in Java
Interpreter implementation in Java
Make Blackjack in Java
How to call functions in bulk with Java reflection
How to create a Java environment in just 3 seconds
Rock-paper-scissors app in Java
[Java] How to omit the private constructor in Lombok
Constraint programming in Java
Sample code to convert List to List <String> in Java Stream