JDK-4211687 : (spec thread) Thread does not belong to the correct ThreadGroup
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 1.1.7
  • Priority: P4
  • Status: Closed
  • Resolution: Cannot Reproduce
  • OS: generic
  • CPU: generic
  • Submitted: 1999-02-15
  • Updated: 2005-06-17
  • Resolved: 2005-06-17
Related Reports
Relates :  
Description

Name: dbT83986			Date: 02/15/99


According to the Spec's at the URL below. When a new Thread is started and 
ThreadGroup is not specified. Then the Thread should belong to the Parent's Thread Group.

http://java.sun.com/docs/books/jls/html/javalang.doc18.html#2658

This is not true in
JDK1.1.6, JDK1.1.7, JDK1.1.7A, JDK1.2 RC1

Steps to recreate
1. compile Applet1.java
2. Set Empty CLASSPATH
3. use appletviewer to view Applet1.html

The cause of the problem is in Thread.java. If parameter ThreadGroup g is null
Then we should *ALWAYS* use parent.getThreadGroup() as the ThreadGroup for 
this thread, irrespective of SecurityManager is set or not.

    private void init(ThreadGroup g, Runnable target, String name){
	Thread parent = currentThread();
	if (g == null) {
	    /* Determine if it's an applet or not */
	    SecurityManager security = System.getSecurityManager();
	    
	    /* If there is a security manager, ask the security manager
	       what to do. */
	    if (security != null) {
		g = security.getThreadGroup();
	    }

	    /* If the security doesn't have a strong opinion of the matter
	       use the parent thread group. */
	    if (g == null) {
		g = parent.getThreadGroup();
	    }
	}

	/* checkAccess regardless of whether or not threadgroup is
           explicitly passed in. */
	g.checkAccess();	    

>>>>	this.group = g;
	this.daemon = parent.isDaemon();
	this.priority = parent.getPriority();
	this.name = name.toCharArray();
	this.target = target;
	setPriority(priority);
	g.add(this);
    }



Here's the source
------------------Applet1.java-------------------
import java.awt.*;
import java.applet.*;
import java.lang.*;

public class Applet1 extends Applet
{
	public void init()
	{
		setLayout(null);
		setSize(426,266);
		ThreadGroup group = new ThreadGroup("Group 1");
	    ThreadA threadA = new ThreadA();
	    new Thread(group,threadA).start();
	}
}

class ThreadA implements Runnable {
	public void run(){
	    System.out.println("Thread Group for ThreadA : " + Thread.currentThread().getThreadGroup());
	    ThreadB threadB = new ThreadB();
	    new Thread(threadB).start();
	}
}

class ThreadB implements Runnable {
	public void run(){
	    System.out.println("Thread Group for ThreadB : " + Thread.currentThread().getThreadGroup());
	}
}
----------------------Applet1.html---------------
<HTML>
<HEAD>
<TITLE>Autogenerated HTML</TITLE>
</HEAD>
<BODY>
<APPLET CODE="Applet1.class" WIDTH=430 HEIGHT=270></APPLET>
</BODY>
</HTML>
--------------------------------------------------

-- dims

###@###.###
(Review ID: 42061)
======================================================================

Comments
EVALUATION The substatus should be "no longer reproducible." This was a valid discrepancy between the original version of the JLS and the Java implementations mentioned in the description. For J2SE version 1.4 the Thread API specification was updated to reflect security manager interaction with a Thread constructor call with a null ThreadGroup parameter as follows: "If group is null and there is a security manager, the group is determined by the security manager's getThreadGroup method. If group is null and there is not a security manager, or the security manager's getThreadGroup method returns null, the group is set to be the same ThreadGroup as the thread that is creating the new thread." ###@###.### 2005-06-17 20:48:02 GMT
17-06-2005

WORK AROUND Name: dbT83986 Date: 02/15/99 Specify ThreadGroup explicitly everywhere you need the correct hierarchy. ======================================================================
11-06-2004