简体   繁体   English

2个线程同时运行的睡眠时间好多少

[英]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. 我正在编写代码以读取大型JSON文件并将数据写入数据库。 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. 我正在旋转2个线程,一个从文件中读取(蒸汽和对象的混合模式,使用Gson一对一读取),将对象放入阻塞队列,第二个线程从队列中读取数据,并使用1000的批处理大小保存到db中。另一方面,线程2不会在线程1中放置任何睡眠计时器,而在保存数据之前,线程2正在使用睡眠(200)。

I was wondering if it will be helpful to put a sleep of 10-20 miliseconds in thread 1 too? 我想知道在线程1中睡眠10-20毫秒是否也会有所帮助? 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). 在不运行基准的情况下很难说出任何性能,但是...原则上,您根本不需要sleep() (如果队列已满,生产者将阻塞,如果队列为空,则生产者将阻塞)。

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. 您可以使用yield()来暗示当前线程到达了一个好点,切换到另一个线程可能是一个好主意。

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 . 即使只有一个核心,也不需要sleep Let your parser put whole batches in the queue, let your DB writer take them. 让您的解析器put 整个批处理放入队列中,让您的DB编写器take它们。 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. 由于JDBC连接是同步的,因此DB编写器可能会花费大部分时间来等待事务提交。

I'm not a Java developer, but the principles are same. 我不是Java开发人员,但是原理是相同的。

  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. 在生产者-消费者模式中,第一个线程(生产者)尽可能快地从JSON读取数据,并将其放入线程安全队列中,第二个线程(消费者)从中读取数据。

Simplified algorithm. 简化算法。

Producer thread. 生产者线程。

  1. Read data from JSON file. 从JSON文件读取数据。
  2. Write read data to queue. 将读取的数据写入队列。
  3. Set synchronization event to notify consumer thread. 设置同步事件以通知使用者线程。
  4. Go to 1. 转到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. 如果队列非空,请转到2,否则请转到1。

The producer-consumer is a well known pattern, so it's not hard to find same examples in Java. 生产者-消费者是众所周知的模式,因此在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. 他们应该改用同步,锁,缓冲区(队列,特别是有限长度的队列)以确保正确的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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