Development

How to process multiple files simultaneously using Java Multithreading

Istock 1194668332

Overview

This use case describes how to process multiple files simultaneously by assigning number of threads on runtime.

 

Advantage

  • Can assign N number of threads on runtime based on CPU utilization.
  • Can process all the files in less time.

 

Required applications to work on this use case

  • Using command prompt
  • Any IDE for Java

 

Problem definition

This document helps to solve technical challenges while processing multiple files by assigning threads to each file and process simultaneously.

 

Sample files to be processed

Files

Code

  • Get the list of all files in Array.
  • Assign number of threads.
  • Assign files equally to each thread.
  • Assign remaining files to the last thread.
  • Run all the threads and wait to complete execution of all the threads.

 

Code1

Code2

Console Output:

Output2

About the Author

Jeyanth Raja Selvan is a senior technical consultant for Perficient India. He has over 9 years of experience in software development.

More from this Author

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe to the Weekly Blog Digest:

Sign Up
Categories