Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4234402

(thread) Thread.setCPUAffinity?



    • Type: Enhancement
    • Status: Closed
    • Priority: P4
    • Resolution: Won't Fix
    • Affects Version/s: 1.2.1, 1.2.2, 1.3.0, 1.4.2, 6
    • Fix Version/s: None
    • Component/s: core-libs
    • Labels:
    • Subcomponent:
    • CPU:
      generic, x86
    • OS:
      generic, windows_2000, windows_xp


      Name: vi73552 Date: 04/30/99

      Could you please add to the system properties, an indication of how many processors the underlying system is using. For multi-threaded, CPU intensive applications, this can be useful information.
      (Review ID: 57682)

      Name: rlT66838 Date: 08/31/99

      The proposal is as follows:

      1. Add method in class System:

      int getNumCPUs(). It returns the number of CPUs, if the platform supports discovering the number of CPUs using some native system call or another, or else if the platform does not allow it, returns 1.

      2. Add method in class Thread:

      void setCPUAffinity(int id). It sets the thread to run on the identified CPU, numbered from 1 to the number returned by System.getNumCPUs; if you pass it 0 it sets the thread to the default behavior on the system, likely that the thread is moved around by the OS to balance load. If the id is bigger than the number of available CPUs it is reduced modulo the number of available CPUs. (This may be useful when spawning threads in a loop and trying to balance them or group them a certain way.) If the id is negative or the thread is dead, an illegalArgumentException is thrown.
      This, of course, only applies if the platform has native threads and the native threads can be set on a specific CPU. Otherwise this is a no-op. It may be necessary to convert between a Java CPU-id and a platform-dependent number or handle.
      The default CPU affinity for a thread will be 0.

      3. (optional) Add method in class Thread:

      int getCPUAffinity(). This returns the thread affinity value of the thread. So at the end of this method:

      void myMethod (Thread aThread, int a) {
        int b = aThread.getCPUAffinity();

      it should be an identity that b == a unless a was out of range or the thread was no good (e.g. null or dead).

      ALTERNATIVELY: The default affinity of a thread will be that of its threadgroup, the affinity of the main threadgroup will be 0 initially, threadgroups will have analogous methods to threads for CPU affinity (a threadgroup affinity change will affect threads with affinity 0 only, and a thread with affinity 0 inherits the threadgroup's affinity); a threadgroup with a parent and affinity 0 inherits the parent's affinity; and the affinities of things like the gc thread are implementation-dependent, and will probably either all be 0 or be chosen by the runtime startup to balance the load caused by the major daemon threads.
      (Review ID: 94566)


          Issue Links



              psoper Pete Soper (Inactive)
              vasya Vassili Igouchkine (Inactive)
              0 Vote for this issue
              2 Start watching this issue