Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java multi-thread application that reads a single file

I currently wrote my program to use 32 threads and read 1 file per thread (so 32 .txt files). The multi-threading has nothing to do with CPU speed, but making 32 calls to BING's api per second is a lot faster than making 1. Each of the .txt files containing a list of search queries. I create a thread it reads one line at a time from it's file. Is it possible to create all 32 threads and point them to a single .txt file??

like image 397
chrstahl89 Avatar asked Mar 08 '26 12:03

chrstahl89


1 Answers

Use the Producer-Consumer pattern. Have only one thread that reads file and pushes each line/command into ArrayBlockingQueue (thread safe read and write) using put().

All other 32 threads should read from the same queue object by calling take(). They will block if queue is empty, which is nice.

This solution is better because the disk is inherently single-threaded so you won't get much by reading the file concurrently.

like image 78
Tomasz Nurkiewicz Avatar answered Mar 10 '26 00:03

Tomasz Nurkiewicz