Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi threading in java

if i have a file contains 4000 bytes, can i have 4 threads read from the file at same time? and each thread access a different section of the file.

thread 1 read 0-999, thread 2 read 1000 - 2999, etc.

please give an example in java.


1 Answers

The file is very very small, and will be very fast to load. What I would do is create a thread-safe data class that loads the data. Each processing thread can then request an ID from the data class and receive a unique one with a guarantee of no other thread sending the same ID to your remote service.

In this manner, you remove the need to have all the threads accessing the file, and trying to figure out who has read and sent what ID.

like image 107
Kieveli Avatar answered Apr 29 '26 00:04

Kieveli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!