Bala
Bala

Reputation: 1365

How to check whether threads in Java program are running sequentially or parallel?

I have a java program which extracts some files onto client machine. I have implemented 4 threads to extract files. But thread implementation barely effecting the extraction time. I have checked thread execution in jvisualvm, it looks fine and I am sure that threads are implemented parallel fashion . Here is my system configuration...

Windows XP, Core2duo, 3GB RAM. java-vm-args : -Xmx512M -Xss2M. I have printed available processors to the running JVM... Runtime.getRuntime().availableProcessors() = 1( If I am not wrong it should execute 2 logical threads).

Am I missing something? How can we assure that threads are running parallel at Hardware level?

Upvotes: 0

Views: 3464

Answers (4)

Gray
Gray

Reputation: 116858

How can we assure that threads are running parallel at Hardware level?

I think this is mostly an invalid question. Under Linux you can see the threads and their state with ps -eLfl but that won't work on other OSs. A thread dump will show you thread status. Going thru the Threads tab in jconsole and clicking on the threads will also show their state.

But thread implementation barely effecting the extraction time

As mentioned by others, this is a textbook sign that your program is IO bound. All of your threads are contending for the same IO resource and are spending most of their time in wait queues.

To test this theory, move your application to a faster disk system. SSDs are the bomb here. Loading your file into memory first and then running the processing with different number of threads will also isolate IO bandwidth problems. Using a memory file system will as well.

If you process is IO bound then there may not be any way to speed it up without increasing your IO bandwidth.

Upvotes: 1

Stephen C
Stephen C

Reputation: 718758

I don't think there is a way to check that apart from by external means; e.g.

  • looking the Windows Task Manager performance graphs, or
  • attaching VisualVM to the JVM.

I have printed available processors to the running JVM:

  Runtime.getRuntime().availableProcessors() = 1

That means that you that the JVM thinks it only has 1 processor available to it. If you actually have two physical cores (or HT virtual cores) then the operating system is only letting the JVM use one of them.

(A bottleneck of some kind in your code wouldn't cause availableProcessors() to return 1.)


How can we assure that threads are running parallel at Hardware level?

You need to make sure that the operating system allows the JVM to use multiple cores.

Upvotes: 0

npe
npe

Reputation: 15699

What you are missing is that the extraction time consists not only of CPU time, but also of HDD reads and writes. Those two take several orders of magnitude more time, than the ZIP algorithm executed by CPU.

Upvotes: 0

Mikhail Vladimirov
Mikhail Vladimirov

Reputation: 13890

Parallel execution helps when CPU is bottleneck. In your case bottleneck is probably I/O, not CPU. Also, you have 1 physical core with hyper-threading. In this case parallel execution will probably not help anyway, even when CPU is bottleneck. Moreover, in some situations, two-threaded application may be slower on single-core, hyper-threading CPU than single-threaded application. Hyper-threading is mostly marketing trick. Do not rely on it.

Upvotes: 0

Related Questions