Reputation: 231
I am writing code to read a large JSON file and write data in database. I am spinning 2 threads, one to read from file (mixed mode of steam and object reading one by one using Gson) placing object into a blocking queue and second thread reads data from queue and save into db using batch size of 1000. I didn't put any sleep timer in thread 1, on the other hand thread 2 is using sleep (200) just before saving data.
I was wondering if it will be helpful to put a sleep of 10-20 miliseconds in thread 1 too? Does it help performance wise switching between threads by using sleep?
Upvotes: 0
Views: 92
Reputation: 48692
None. You should not rely on sleep to ensure multiple threads interact correctly. They should instead use synchronization, locks, buffers (queues, especially finite length queues) to ensure correct operation.
Upvotes: 0
Reputation: 1232
I'm not a Java developer, but the principles are same.
In producer-consumer pattern 1st thread (producer) reads data from JSON as fast as possible and putting them to thread safe queue from which 2nd thread (consumer) reads them.
Simplified algorithm.
Producer thread.
Consumer thread.
The producer-consumer is a well known pattern, so it's not hard to find same examples in Java. For instance look here.
Upvotes: 0
Reputation: 46482
Do you have just one core? Or are other cores busy? Otherwise, just drop the sleep, you don't need it.
Even with a single core, you don't need sleep
. Let your parser put
whole batches in the queue, let your DB writer take
them. I'd go for a queue size of two, so that the DB improbably idles needlessly.
As the JDBC connection is synchronous, the DB writer probably spends most time waiting for the transaction commit.
Upvotes: 0
Reputation: 1778
It's hard to say anything about performance without running benchmarks, but... in principle, you shouldn't need to sleep()
at all (the producer will block if the queue is full, the consumer if it's empty).
You can use yield()
to hint that the current thread reached a point where it might be a good idea to switch to another one.
Upvotes: 1