1

This is going to be the most basic and even may be stupid question here. When we talk about using multi threading for better resource utilization. For example, an application reads and processes files from the local file system. Lets say that reading of file from disk takes 5 seconds and processing it takes 2 seconds.

In above scenario, we say that using two threads one to read and other to process will save time. Because even when one thread is processing first file, other thread in parallel can start reading second file.

Question: Is this because of the way CPUs are designed. As in there is a different processing unit and different read/write unit so these two threads can work in parallel on even a single core machine as they are actually handled by different modules? Or this needs multiple core.

Sorry for being stupid. :)

Walt
  • 1,426
  • 2
  • 17
  • 30
  • So what is your question exactly? Is it "Can multithreading improve performance in case there is only one core?"? – kraskevich Jan 22 '15 at 11:22
  • My question is how exactly these two threads (one that reads from disk and other that processes) run in parallel on a single core machine – Walt Jan 22 '15 at 11:24
  • If one file is read. Can one thread process it in parallel to other thread reading the second file ? – Walt Jan 22 '15 at 11:30
  • This answer might be interesting: http://stackoverflow.com/a/5150840/1134700 – Peter Bagyinszki Jan 22 '15 at 12:45
  • In a single core processor, there is no parallelism at all. Threads and processes may appear to run in parallel, but in reality everything is serial – kazanaki Jan 22 '15 at 14:13
  • Did you get a correct answer for this question? – Aman Gautam Jan 26 '15 at 09:58

3 Answers3

2

On a single processor, multithreading is achieved through time slicing. One thread will do some work then it will switch to the other thread.

When a thread is waiting on some I/O, such as a file read, it will give up it's CPU time-slice prematurely allowing another thread to make use of the CPU.

The result is overall improved throughput compared to a single thread even on a single core.

Key for below:

  • = Doing work on CPU
  • - I/O
  • _ Idle

Single thread:

====--====--====--====--

Two threads:

====--__====--__====--__
____====--__====--__====

So you can see how more can get done in the same time as the CPU is kept busy where it would have been kept waiting before. The storage device is also being used more.

weston
  • 54,145
  • 21
  • 145
  • 203
1

In theory yes. Single core has same parallelism. One thread waiting for read from file (I/O Wait), another thread is process file that already read before. First thread actually can not running state until I/O operations is completed. Rougly not use cpu resource at this state. Second thread consume CPU resource and complete task. Indeed, multi core CPU has better performance.

Erdinç Taşkın
  • 1,548
  • 3
  • 17
  • 28
  • If one file is read. Can one thread process it in parallel to other thread reading the second file ? – Walt Jan 22 '15 at 11:29
1

To start with, there is a difference between concurrency and parallelism. Theoretically, a single core machine does not support parallelism.

About the question on performance improvement as a result of concurrency (using threads), it is very implementation dependent. Take for instance, Android or Swing. Both of them have a main thread (or the UI thread). Doing large calculation on the main thread will block the UI and make in unresponsive. So from a layman perspective that would be a bad performance.

In your case(I am assuming there is no UI Thread) where you will benefit from delegating your processing to another thread depends on a lot of factors, specially the implementation of your threads. e.g. Synchronized threads would not be as good as the unsynchronized ones. Your problem statement reminds me of classic consumer producer problem. So use of threads should not really be the best thing for your work as you need synchronized threads. IMO It's better to do all the reading and processing in a single thread.

Multithreading will also have a context switching cost. It is not as big as Process's context switching, but it's still there. See this link.

[EDIT] You should preferably be using BlockingQueue for such producer consumer scenario.

Community
  • 1
  • 1
Aman Gautam
  • 3,549
  • 2
  • 21
  • 25
  • Thanks a lot for replying. So, does that mean even if I use two threads one for reading file from disk and other for processing file, at any instant only one thread will be doing its work ? Or is it possible that one thread can be reading while other is processing the previously read file at the same time ? PS: Assuming this to be a single core machine. – Walt Jan 22 '15 at 14:31
  • If both your threads would be working on the same data. i.e if you will be reading and writing in the same variable, it would be a lot of trouble. LOT of trouble. If you will use separate variables and wait till there is data there, you will again be waiting for the other thread (synchronized). So performance will not be great. It's easiest to go for a single threaded model in simple scenario like this. – Aman Gautam Jan 22 '15 at 15:16
  • Best way to judge it is using some benchmarks :) It would be great if you can do some benchmarking and share the result with rest of us. Thanks – Aman Gautam Jan 22 '15 at 15:17
  • Please check the Edit. it may be helpful to use a BlockingQueue implementation in your case. – Aman Gautam Jan 24 '15 at 01:57