I've been writing an application for matrix multiplication.
I've got it done and watched resource monitor for comparison.
First of all
3000x3000 matrixes multiplied.
Single threaded was slower than multi threaded
When I check Windows resource monitor, I see that multithreading app has more threads than single threaded. I checked "javaw.exe" and even if I write single threaded app, it has more threads than one. That's not about me. That's about "javaw.exe" itself. But long story short, javaw with single thread showed - for example - 16 threads. Multi threaded showed - for example - 24.
While multi threaded app working, CPU use was almost 100%. Most of use belong to "javaw.exe". But in single threaded app, the use was around 30-35%
I've a i5 CPU. Dual core. 4 cores logical.
When I check Windows resource monitor, for multi thread app, CPU 0-1-2-3 use was almost 100%, again.
But in single thread app, CPU 0-1-2-3 was still using. Around same percentages, but not even close to 100%.
Here goes my question. When I was executing single threaded app, who was using the other cores? Of course any other process can use them. Or even "javaw.exe" itself (I told myself, it was multithreaded). But is there any possibility that JVM executes my single threaded process as multi threaded?
But is there any possibility that JVM executes my single threaded
process as multi threaded?
No. The JVM will have multiple threads but it won't and can't just decide to multithread your program unless you create the threads yourself (or use some other multithreading mechanism like Executor).
Related
This is what I see in Oracle documentation and would like to confirm my understanding (source):
A computer system normally has many active processes and threads. This
is true even in systems that only have a single execution core, and
thus only have one thread actually executing at any given moment.
Processing time for a single core is shared among processes and
threads through an OS feature called time slicing.
Does it mean that in a single core machine only one thread can be executed at given moment?
And, does it mean that on multi core machine multiple threads can be executed at given moment?
one thread actually executing at any given moment
Imagine that this is game where 10 people try to sit on 9 chairs in a circle (I think you might know the game) - there isn't enough chairs for every one, but the entire group of people is moving, always. It's just that everyone sits on the chair for some amount of time (very simplified version of time slicing).
Thus multiple processes can run on the same core.
But even if you have multiple processors, it does not mean that a certain thread will run only on that processor during it's entire lifetime. There are tools to achieve that (even in java) and it's called thread affinity, where you would pin a thread only to some processor (this is quite handy in some situations). That thread can be moved (scheduled by the OS) to run on a different core, while running, this is called context switching and for some applications this switching to a different CPU is sometimes un-wanted.
At the same time, of course, multiple threads can run in parallel on different cores.
Does it mean that in a single core machine only one thread can be executed at given moment?
Nope, you can easily have more threads than processors assuming they're not doing CPU-bound work. For example, if you have two threads mostly waiting on IO (either from network or local storage) and another thread consuming the data fetched by the first two threads, you could certainly run that on a machine with a single core and obtain better performance than with a single thread.
And, does it mean that on multi core machine multiple threads can be executed at given moment?
Well yeah you can execute any number of threads on any number of cores, provided that you have enough memory to allocate a stack for each of them. Obviously if each thread makes intensive use of the CPU it will stop being efficient when the number of threads exceeds the number of cores.
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. :)
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.
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.
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.
I'm studying a course on Operating systems and I reached a part where it discusses processes and threads. I know a CPU can only run a single process at a time, so there are several scheduling algorithms out there to priorities the processes in the Ready queue.
Now when I moved to threads things started to get somewhat confusing. Since a process may consist of several threads will the scheduling be for each single thread or for each process?
For example:
I'm on Windows. I double click a song to start it in VLC then double click MS Word to start writing a report and finally open Chrome to check my mail.
Lets assume the following to simplify things:
VLC process has only a single thread.
MS Word has 5 threads in its process.
Chrome creates a thread for each tab I open in the browser.
All these programs are written in Java and all threads in these programs are created using the Thread class.
Now which of these is a User thread and which is a Kernel thread?
Will scheduling be on the processes or on the threads?
Will the processes with higher number of threads run longer or is the operating system ignorant of the number of threads in each process?
Applications are developed via different languages and the different languages implement threading differently. There are basically 2 different implementations.
Create separate Kernel Thread for each thread created in the application.
Manage the application created thread within a main thread of application.
Note : Java's implementation could also vary JVM to JVM so it depends that which JVM and OS are used.
Coming to your next question. Scheduling will be on Threads not on Processes.
Can a single thread of a Java program automatically make use of multiple cores on the CPU?
Can a single thread of a Java program automatically make use of multiple cores on the CPU?
Yes and no. A single threaded Java program will use multiple threads in that the GC, JMX, finalizer, and other background threads can run in different CPUs (whether CPU or core). The GC threads especially give a significant performance boost if they can be running in another CPU. However, your single threaded application code, although it may move between CPUs, will never be running in 2 CPUs at the same time.
how to find out that?
That's a harder question and it depends on what architecture you are running on. ps under *nix will be able to show if you have multiple threads in the run queue but even then it may not show that they are actually executing in multiple CPUs.
Your own code will not run on multiple cores if it is by definition single threaded. No single threaded application can run simultaneously on multiple cores - unless your using underluing multithreaded calls/libraries without knowing.
Usually gc is running in a separate thread. But usually it doesn't make any significant difference. That's all.
I read that sleep() puts the currently running thread to sleep. Is this valid for multi-core processors also? I am not sure, but it think a multi-core processor would have multiple "currently running threads".
I read that Sleep() puts the currently running thread to sleep. Is this valid for multi core processors also?
Yes. sleep(...) would put it's executor thread for the sleep for the time passed as argument.
but it think a multi core processor would have multiple "currently running threads".
True. A multicore processor will have more than one threads running at same instant of time.
What is the currently running thread on a multicore processor?
See this: Semantics of Thread.currentThread() on multicore/multi processor systems?
Thread.CurrentThread() will be executed on certain thread. The same thread will be returned irrespective of that on which processor it is running.
sleep only puts the thread to sleep on which it was called.
And yes, that's also true on multi-core processors as multi-threading doesn't necesarily require a multicore pc. A single core processor can hanle multiple processes as well, can't it?
An OS gives 'directions' to a core, or more. So e.g. Windows can give direction to multiple cores. Windows can run multiple processes. Time slicing gives each process a certain amount of time to perform its task. That is, a process is given a certain amount of time to perform its calculations on the core. As Windows (in this example) is preemptive (as so is linux), it will decide which process will be put on the core and for how long.
Now a process can have multiple threads. Each thread is like a single 'process' on the CPU. So again, a pre emptive OS decides which thread runs when. On a quad (4) core, you can only run 4 process/threads at the same time. On a single core you would be able to run 4 processes as well, but only one at the time.
Example:
When you have 4 processes with each 2 threads on a quad core. Windows can decide to put one process on each core. Windows will decide that each core will switch between the 2 threads of the process that is assigned to that core. Now when you put one of those two threads to Sleep, Windows does not have to switch between threads for the time you have put your thread to sleep, and can spent all its time performing one thread. While at the same time the switching of threads continues for the other 3 processes that where loaded on the other cores.
Of course its far more complex than I summarized above.