Skip to main content

Posts

Showing posts with the label Asynchronous execution

How to read a large file in Java

Problem with reading large file Large file can be any plain text or binary file which is huge in size and can not fit in JVM memory at once. For example if a java application allocated with 256 MB memory and it tries to load a file completely which is more or close to that memory in size then it may throw out of memory error. Points to be remembered Never read the whole file at once. Read file line by line or in chunks, like reading few lines from text file or reading few bytes from binary file. Do not store the whole data in memory, like reading all lines and keeping as string.  Java has many ways to read the file in chunks or line by line, like BufferedReader, Scanner, BufferedInputStream, NIO API. We will use NIO API to read the file and Java stream to process it. We will also see how to span the processing with multiple threads to make the processing faster. CSV file In this example I am going to read a CSV file which is around 500 MB in size.  Sample is as...

Asynchronous execution using CompletableFuture in java

CompletableFuture CompletableFuture was introduced in Java 8 to support the asynchronous execution and avoid blocking calls. It implements Future and CompletionStage interfaces. Future can be used to retrieve the value and status of current task while CompletionStage provides multiple methods to support the event based task execution which helps in creating a chain or pipeline for the actions to happen during specified events. Runnable Runnable interface has a void run() method where we can write the logic which we want to execute but it can not return any result. Runnable can be executed using Thread or ExecutorService. public abstract void run(); When we need to execute some tasks where we don't need to wait to get some result back then we can use Runnable. We just execute our task and do other work as we don't depend on the result of the task. Like if we want to write some logs asynchronously then we can use Runnable interface and execute without waiting for it...