Multithreading on Dual Core MAChine

multithreading on dual core machine?

The term threads usually covers three abstraction layers:

  1. User threads are threads launched by applications and are mapped N:M to:
  2. Kernel threads, which are threads managed by the operating system, mapped N:M to:
  3. Hardware threads, which are the actual physical resources available.

The 4 threads you said are launched by the application are from category 1 (user threads), while the value 2 returned by that function refers to category 3 (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.

Having said this, typically starting more than 2x the number of hardware threads if you are doing intensive computations will hurt performance due to context switches and resource contention.

How many java threads can be created for a dual core processor

You can run 20 threads even on a single-core machine.
What happens is called time slicing.

http://en.wikipedia.org/wiki/Time_slice#Time_slice

It is a way for the processor to simulate multiple
processors executing multiple tasks as once.

Thread Cooperation on Dual-CPU Machines

Yes, the introduction of dual-core CPUs made a significant number of programs with latent threading races fail quickly. Single-core CPUs multitask by the scheduler rapidly switching the threading context between threads. Which eliminates a class of threading bugs that are associated with a stale CPU cache.

The example you give can fail on a single core as well though. When the thread scheduler interrupts the thread just as it loaded the value of the variable in a register in order to increment it. It just won't fail nearly as frequently because the odds that the scheduler interrupts the thread just there isn't that great.

There's an operating system feature to allow these programs to limp along anyway instead of crashing within minutes. Called 'processor affinity', available as the AFFINITY command line option for start.exe on Windows, SetProcessAfinityMask() in the winapi. Review the Interlocked class for helper methods that atomically increment and decrement variables.

Dual-core CPU utilization w/ single Java thread running

When the OS executes threads, it runs each thread for a certain period of time (say 10-20ms), then saves the state of the thread, and looks for other threads to run.

Now, despite what you might think from looking at the CPU Utilization graph, the OS is actually running a lot more threads than those from your program. There are threads running UI loops, threads waiting on I/O, threads running background services, etc. Most of the threads spend most of their time blocked waiting on something.

The reason why I'm talking about this is to explain that from OS's point of view, the situation is more complex than it might look. There are a whole bunch of threads doing a whole bunch of things, and the OS is attempting to switch between them. Suppose that you wanted to implement a heuristic that if a thread used up its entire quantum the last time, then the OS will make an effort to schedule it to the same core. The OS needs to track and take into account more information, and the success of the optimization may depend on a lot of hard-to-predict factors.

In addition, the benefit of affinitizing a thread to a core are often negligible in practice, so OSes don't attempt to do it automatically. Instead, they expose a feature that allows the developer to explicitly say that a particular thread should be affinitized to a core, and then the OS will respect the decision.

That seems like a reasonable trade-off: if your thread performs better when affinitized to a core, just ask the OS to do that. But, the OS won't bother attempting to figure it out for you.

Can someone help me answer to this question

Depending on hardware with Hyperthreading 1 core would support 2 threads. So you will have a total of 4 threads supported by your cpu. Probably the answer will be aproximaty 25(even with hyperthreading it does not run exactly 2 times faster).

Without hyperthreading 50

How does multiple threaded application works in multi processor system

is there any possibility for the car thread and bike thread to run simultaneously at any point of time?

Yes, there is a possibility. It depends on the JVM implementation (since you mentionned Java) and the OS implementation, so it's not garanteed, but it's possible.

As we know only one thread runs at a time. (context switch happens between threads very fast which makes it look like it is running parallel to human eyes)

That's wrong. It would be true on a single core single processor, and could be true on some JVM/OS, but is wrong in the general case.

See JLS chapter 17:

Threads may be supported by having many hardware processors, by time-slicing a single hardware processor, or by time-slicing many hardware processors.

Java multi-threading on an Intel core i5 processor

Java parallel stream uses ForkJoinPool.commonPool; which calculates total threads by Runtime.getRuntime().availableProcessors() - 1 (this is for leaving one processor calling thread).

So in your case it would be 12 -1 = 11 processors.

So you might be able to use 11 threads for your multithreading operations.

E.g. on my system I do have 8 available processors & I can see below operations is being performed by 7 threads:

System.out.println(Runtime.getRuntime().availableProcessors());
IntStream.range(0, 1000000).parallel()
.forEach(value -> System.out.println(value + " " + Thread.currentThread().getName()));


Related Topics



Leave a reply



Submit