1

In a parallel Java code can we learn which core a thread is executed on?

Say that I have 10 threads and 4 cores, is it possible to learn which core is used for thread 1, thread 2, thread 3 etc in code?

It is not a crucial problem but I wonder if it is possible.

ecem
  • 3,574
  • 3
  • 27
  • 40
  • 1
    Very unlikely... Java has no such low level access, and anyway it is the OS which will tell you ultimately, and it is not sure whether the physical layout of your CPUs is even mentioned! – fge May 30 '13 at 22:50
  • You can infer your answer from this Q/A: http://stackoverflow.com/q/1896065/1065197 – Luiggi Mendoza May 30 '13 at 22:52
  • 1
    What are you trying to do? Why do you need this information? – Gray May 30 '13 at 22:56
  • Like @Gray infers, why would you need such info? Even if you could get it, it would be stale by the time any such enquiry returned/signaled. – Martin James May 31 '13 at 00:38
  • I know, I was just performing time measurements on the parallel code and at one instance of execution, one of the threads finished nearly 10 seconds after the rest of the threads finished. I was not monitoring the CPUs at that time, therefore I wondered if I could have learnt which core was executing that specific thread from the code. Anyways, thank you all! – ecem May 31 '13 at 09:22

1 Answers1

2

In a parallel Java code can we learn which core a thread is executed on?

From inside of Java, the answer is no. There are ways to look at the process list in Linux (and maybe other Unixen) that could show you the virtual processes and maybe CPU affinity. This is very non-portable however and for tasks that are context switching, it is not going to be meaningful.

I have some more details about CPU info here but it doesn't address the affinity question: Concurrency of posix threads in multiprocessor machine

Community
  • 1
  • 1
Gray
  • 115,027
  • 24
  • 293
  • 354