[Bluej-discuss] Thread Question
Mark Dutchuk (MALA)
dutchukm at MALA.BC.CA
Sun Nov 12 22:57:39 GMT 2006
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.
--
Donald (Mark) Dutchuk,
Instructor / Program Coordinator,
Information Technology Applied Systems (ITAS) Program
Malaspina University/College
Room 226 - Building 180
900 - 5th Street
Nanaimo, BC CAN V9R 5S5
Telephone: (250) 740-6116
More information about the bluej-discuss
mailing list