简体   繁体   中英

How much sleep time is good for 2 threads running simultaneously

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?

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.

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.

I'm not a Java developer, but the principles are same.

  1. Don't use sleeps.
  2. Use producer-consumer pattern.

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.

  1. Read data from JSON file.
  2. Write read data to queue.
  3. Set synchronization event to notify consumer thread.
  4. Go to 1.

Consumer thread.

  1. Wait until synchronization event is set.
  2. Read data from queue.
  3. Save data to DB.
  4. If queue is non-empty go to 2 else go to 1.

The producer-consumer is a well known pattern, so it's not hard to find same examples in Java. For instance look here .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM