Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 5073365
Votes 0
Synopsis (thread) java.lang.Thread.SetPriority() raises a NullPointerException if a thread has exi
Category java:classes_lang
Reported Against 1.2.1 , 1.4.2 , merlin-beta3
Release Fixed mustang(b43)
State 10-Fix Delivered, bug
Priority: 3-Medium
Related Bugs 4515956 , 5037861
Submit Date 13-JUL-2004
Description

Compile and run the following program.
The setPriority method call will raise a NullPointerException.

public class Npe extends java.lang.Thread {

    public void run(){
        try{
            Thread.sleep(500);
        }catch(java.lang.InterruptedException e){}
    }

    public static void main(String[] args){
        Npe npe = new Npe();

        npe.start();
        try{
            npe.join();
        }catch(java.lang.InterruptedException e){}

        npe.setPriority(java.lang.Thread.MIN_PRIORITY);
    }

}

The error message:

Exception in thread "main" java.lang.NullPointerException
    at java.lang.Thread.setPriority(Thread.java:875)
    at Npe.main(Npe.java:18)

obviously the line number in Thread.java will vary by build

======================================================================
Posted Date : 2005-07-25 13:58:33.0
Work Around
Always specify thread priority at thread creation time.
One can argue that thread priority setting is really part of 
thread "construction".

  xxxxx@xxxxx   2004-07-13
Evaluation
The NullPointerException is clearly unacceptable.

Perhaps the spec should be changed so that setPriority must be
called before start().

  xxxxx@xxxxx   2004-07-13

A setPriority invocation for a terminated thread will no longer throw NPE. Care was taken to avoid a race between nulling of a thread's group object during its transition to terminated and use of the the object to range check the a new priority.

Changes were integrated after consultation with the submitter. Setting the priority of a terminated thread is just as meaningless as setting the priority of a thread that never gets started (i.e. it is a null operation relative to thread behavior).  Specifying this as illegal imposes a hardship on users (to avoid catching the thread after its state has changed, a potentially difficult task) to take the place of the NPE and there is no real benefit from the restriction. In addition, the terminated thread group's max and min priority being unknowable justifies ignoring the value instead of having the JVM stuff it into the thread state. That is, the new value not being retrievable with getPriority is balanced by the fact that a value that is not valid for the thread group will not be accepted.

The exact change:

*** beforeThread.java	Mon Jul 25 09:43:07 2005
--- afterThread.java	Mon Jul 25 09:43:36 2005
***************
*** 104,110 ****
   * a thread is created, a new name is generated for it. 
   *
   * @author  unascribed
!  * @version 1.157, 06/03/05
   * @see     java.lang.Runnable
   * @see     java.lang.Runtime#exit(int)
   * @see     java.lang.Thread#run()
--- 104,110 ----
   * a thread is created, a new name is generated for it. 
   *
   * @author  unascribed
!  * @version 1.158, 06/10/05
   * @see     java.lang.Runnable
   * @see     java.lang.Runtime#exit(int)
   * @see     java.lang.Thread#run()
***************
*** 974,987 ****
       * @see        java.lang.ThreadGroup#getMaxPriority()
       */
      public final void setPriority(int newPriority) {
  	checkAccess();
  	if (newPriority > MAX_PRIORITY || newPriority < MIN_PRIORITY) {
  	    throw new IllegalArgumentException();
  	}
! 	if (newPriority > group.getMaxPriority()) {
! 	    newPriority = group.getMaxPriority();
! 	}
! 	setPriority0(priority = newPriority);
      }
  
      /**
--- 974,990 ----
       * @see        java.lang.ThreadGroup#getMaxPriority()
       */
      public final void setPriority(int newPriority) {
+         ThreadGroup g;
  	checkAccess();
  	if (newPriority > MAX_PRIORITY || newPriority < MIN_PRIORITY) {
  	    throw new IllegalArgumentException();
  	}
! 	if((g = getThreadGroup()) != null) {
! 	    if (newPriority > g.getMaxPriority()) {
! 		newPriority = g.getMaxPriority();
! 	    }
! 	    setPriority0(priority = newPriority);
!         }
      }
  
      /**
Posted Date : 2005-07-25 13:58:34.0
Comments
  
  Include a link with my name & email   

Submitted On 26-APR-2005
remi_forax
This decision seems to restrictive, at least on my windows
box, it's possible to chnage the priority at runtime and
it's works.

I think that setPriority just need to check if the thread is not already dead i.e check if group != null.



PLEASE NOTE: JDK6 is formerly known as Project Mustang