I was going through a Java tutorial where it was mentioned that actual multithreading doesn't happen in a machine having a single processor. It mentioned that OS allots a specified amount of time for the Java process and JVM thread scheduler picks up threads for running one thread at a time for a small amount of time.
I have a laptop which quadcore processor - it is possible to run a multi-threaded program faster programatically by running one thread in each core? The reason why I am asking this question is because the book mentioned that only a true multi processor system can do multiple things at the same time.
Java will benefit from multiple cores, if the OS distribute threads over the available processors. JVM itself do not do anything special to get its threads scheduled evenly across multiple cores.
If your CPU has four cores, it can handle four threads at once.
In computer architecture, multithreading is the ability of a central processing unit (CPU) (or a single core in a multi-core processor) to provide multiple threads of execution concurrently, supported by the operating system. This approach differs from multiprocessing.
In a multithreaded process on a single processor, the processor can switch execution resources between threads, resulting in concurrent execution. Concurrency indicates that more than one thread is making progress, but the threads are not actually running simultaneously.
Even a single CPU can do "multiple things at the same time" in a loose sense, but they are not truly in parallel. You can start 100 threads to run on a single core and they will get time slices during which each of them can run a few instructions, thus creating the impression that they are all executing at the same time.
As I've said in another SO post: multithreading on dual core machine?
The term threads usually covers three abstraction layers:
Java threads are user threads. The 4 cores in your CPU count as hardware threads. Since the mapping is N:M across the layers, you can see that you can have several user threads mapped to a smaller number of hardware threads.
Now, having said this, there are generally two classes of thread activities, each with their own quirks:
The second class of threads above lets you really see the benefit or running a multithreaded java program on your quad-core CPU. Here is a simple example of a program that executes squaring of 1.000.000.000 numbers first sequentially and then in parallel using a thread pool with 4 threads:
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
class ThreadTask implements Runnable {
private int total = 0;
public ThreadTask(int total) {
this.total = total;
}
@Override
public void run() {
int value = 0;
for(int i = 0; i < total; i++) {
value = i * i;
}
}
}
public class Test {
public static void main(String[] args) throws InterruptedException {
int total = 1000000000;
long start = System.currentTimeMillis();
long value = 0;
for(int i = 0; i < total; i++) {
value = i * i;
}
long stop = System.currentTimeMillis();
System.out.println((stop - start) + " ms");
ExecutorService exec = Executors.newFixedThreadPool(4);
start = System.currentTimeMillis();
for(int i = 0; i < 4; i++) {
exec.submit(new ThreadTask(total / 4));
}
exec.shutdown();
exec.awaitTermination(10, TimeUnit.SECONDS);
stop = System.currentTimeMillis();
System.out.println((stop - start) + " ms");
}
}
Feel free to adjust the value of total
if it's running too fast. Now I'm working on a netbook with Intel Atom, so it not really fast.
Even with only one processor multiple threads can make your program faster it all depends on the work you're trying to speed up. For instance if your threads are waiting for IO. If it's purely computational then you'd probably want to limit your threads to your number of cores.
Measure it test it with experiments.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With