This discussion is archived
5 Replies Latest reply: Feb 9, 2012 1:21 AM by 802316 RSS

Processor affinity

891575 Newbie
Currently Being Moderated
In the search for setting thread processor affinity in Java, I came across this [Will Not Implement|http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4234402] bug showing that setting processor affinity will not be supported in Java.

I have also seen the workaround mentioned in Java Threads using JNI (which 2nd edition is [available for download as PDF|http://download553.mediafire.com/zehh33o2iemg/1e6yfsrcx4lv0vv/O%27Reilly+-+Java+Threads+2nd+Edition.pdf] ).

I am wondering why this isn't implemented in Java since both Windows and Linux support it natively? And if anybody has experience in using the Java Threads model--specifically, on how to identify each Java Thread (tid) as a native thread id (nid)?
  • 1. Re: Processor affinity
    DrClap Expert
    Currently Being Moderated
    I'm surprised you linked to the bug report which pretty clearly explains why it wasn't implemented, and then turn around and ask why it wasn't implemented.
  • 2. Re: Processor affinity
    891575 Newbie
    Currently Being Moderated
    Surprised? That enhancement request and explanation were posted in 1999. This is 2011 almost 2012. The times have changed. Multi-core CPUs are the norm today. In 1999, multi-CPU support was for servers, rare and expensive. Today they're in cell phones and tablet PCs with more coming in 0 to 18 months. I have a "fairly old" AMD X6 1055T desktop PC with 6 cores, for example. Its massive CPU support is essentially wasted in Java without direct affinity support, and it's not the latest.

    I'm just wondering why it hasn't been implemented in 2011 since it's so prevalent and easily supported in every major OS (and most all minor OSes too).

    Edited by: 888572 on Sep 29, 2011 9:59 AM
  • 3. Re: Processor affinity
    796440 Guru
    Currently Being Moderated
    888572 wrote:
    Surprised? That enhancement request and explanation were posted in 1999. This is 2011 almost 2012. The times have changed. Multi-core CPUs are the norm today. In 1999, multi-CPU support was for servers, rare and expensive. Today they're in cell phones and tablet PCs with more coming in 0 to 18 months. I have a "fairly old" AMD X6 1055T desktop PC with 6 cores, for example. Its massive CPU support is essentially wasted in Java without direct affinity support, and it's not the latest.

    I'm just wondering why it hasn't been implemented in 2011 since it's so prevalent and easily supported in every major OS (and most all minor OSes too).
    The response there explained why it wasn't implemented. What part of that resopnse--which specifically referred to a trend that has continued--do you think no longer applies?
  • 4. Re: Processor affinity
    DrClap Expert
    Currently Being Moderated
    I'm certainly in favour of Java using all the processors it can get its hands on, and if it isn't doing that then it should be. However it isn't obvious to me how letting programmers assign threads to processors would be a major improvement. That seems like a niche feature to me.

    And the RFE as posted in that bug report is definitely obsolete and shouldn't be implemented as written anyway. Even assuming that the number of processors available is an integer isn't valid any more.
  • 5. Re: Processor affinity
    802316 Pro
    Currently Being Moderated
    I have written a library which uses JNI and JNA (depending on what is available) for thread affinity. There is also a higher level abstraction so you can allocate cpus or whole cores (if you don't want another thread competing for resources due to hyper threading) and declare which threads should be on the same/different socket/cpu.

    It also support the asm instructions RDTSC (a low latency nano timer) and PAUSE (to improve the efficiency of busy waiting)

    https://github.com/peter-lawrey/Java-Thread-Affinity

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points