[Bluej-discuss] Thread Question
Davin McCall
davmac at bluej.org
Mon Nov 13 03:13:22 GMT 2006
Hi Mark,
The crux of the problem is that the thread that is running is not the
one you are calling getThreadGroup() on. Your program creates a total of
6 thread objects (two of them are instances of EventOne, and one is an
an instance of EventTwo; the others are just plain old Threads). It only
actually starts 3 of them (the plain old Threads).
For example:
EventOne e1 = new EventOne();
// Create a new Thread running under the 'threadGroup' ThreadGroup
new Thread(threadGroup, e1).start();
... actually creates an EventOne object (e1) whose thread group will be
the same thread group as myTest1() is called on. It then creates a
Thread (in another thread group) which calls the "run" method of e1,
which does this:
System.out.println("EventOne: " + getThreadGroup()
... However, the getThreadGroup() call retrieves the thread group of e1
(which as I mentioned before is the same thread group as that which
myTest1() is run on) rather than the thread group of the currently
running thread. To re-iterate: e1 is NOT the currently running thread
and in fact e1 is NEVER actually started as a thread in its own right
(yes, it is a Thread object, but it never actually represents a
thread-of-execution - a real thread - because the "start" method is
never called).
To get the behaviour which (I think) you want, you should add a
constructor to EventOne and EventTwo which takes a thread group as a
parameter and which passes that parameter to the superclass constructor,
something like:
public EventOne(ThreadGroup group)
{
super(group, null);
}
... and then change the code which creates the thread to:
EventOne e1 = new EventOne(threadGroup);
e1.start();
- Davin
Mark Dutchuk (MALA) wrote:
> Hello folks. I'm puzzled by a problem I am having with Threads and ThreadGroups. I've distilled the problem down to the following program to demonstrate the problem:
>
> public class TGTest {
>
> private static ThreadGroup threadGroup = new ThreadGroup("Some Thread Group");
>
> public static void myTest1() {
>
> EventOne e1 = new EventOne();
> // Create a new Thread running under the 'threadGroup' ThreadGroup
> new Thread(threadGroup, e1).start();
>
> EventTwo e2 = new EventTwo();
> new Thread(threadGroup, e2).start();
> }
> public static void myTest2() {
> EventOne e1 = new EventOne();
> new Thread(threadGroup, e1).start();
> }
> }
>
> class EventOne extends Thread {
> public void run() {
> System.out.println("EventOne: " + getThreadGroup());
> }
> }
>
> class EventTwo extends Thread {
> public void run() {
> TGTest.myTest2();
> }
> }
>
> Here's the output when I run TGTest.test1():
>
> EventOne: java.lang.ThreadGroup[name=main,maxpri=10]
> EventOne: java.lang.ThreadGroup[name=Some Thread Group,maxpri=10]
>
>
> Why does EventOne run under the 'main' ThreadGroup when I call myTest1() directly from BlueJ, but under 'Some Thread Group' when EventTwo calls myTest1()? I want it to run under 'Some Thread Group' both times, but I can't see the problem.
>
> btw: The problem has nothing to do with static methods and/or variables. I've rewritten the program both ways (setting everything non-static and creating an instance of TGTest), the outcome is the same. Any help or suggestions would be much appreciated.
>
>
> dmd.
>
More information about the bluej-discuss
mailing list