multithreading - Java: Asynchronous concurrent writes to disk -
i have function in main thread write data disk. don't want main thread stuck (high latency of disk i/o) , creating new thread write overkill. have decided use executorservice.
executorservice executorservice = executors.newfixedthreadpool(3); future future = executorservice.submit(new callable<boolean>() { public boolean call() throws exception { logger.log(level.info, "writing data disk"); return writetodisk(); } });
writetodisk function write disk
is nice way do? refer better approach if any?
update: data size greater 100 mb. disk bandwidth 40 mbps, write operation take couple of seconds. don't want calling function stuck has other jobs, so, looking way schedule disk i/o asynchronous execution of calling thread.
i need delegate task , forget it!
there 2 approaches aware of, spin thread (or use pool of threads have) or memory map file , let os manage it. both approaches, memory mapping file can as 10x faster using java writers/streams , not require separate thread bias towards when performance key.
either way, few tips optimize disk writing try preallocate file possible. resizing file expensive. spinning disks not seeking, , ssds not mutating data has been written.
i wrote benchmarks me explore area awhile back, feel free run benchmarks yourself. amongst them example of memory mapping file.
Comments
Post a Comment