I have a Java class called “CreateThread” which creates one thread, everytime an object is instantiated(i.e. this upper level thread is created in the constructor). This one thread in turn creates three other threads. I can create ‘N’ number of objects. At the first level ‘N’ number of threads are created. I also name this upper level as threads as “one”, “two”, “three” etc. These ‘N’ threads created N*3 number of threads. This scenario I am able to implement. However the problem arises when I need to kill/interrupt the threads.
For example, Thread named “one” created three other threads ‘a’,’b’ and ‘c’. If I want to kill all the above 4 threads(one,a,b,and c), the problem arises. I have no clue how this could be achieved.
The call to kill a thread comes from another class called “KillThread”. Since I do not have a handle to any of the threads created above, I am unable to kill the threads. What I mean is that I want to kill all 4 threads from a different class which doesn’t have a reference to the threads.
Help in solving the above scenario is greatly appreciated.
Thanks, Rajath
Just store the references to the created threads somewhere. The main class could maintain a list for the N threads, and each N thread could maintain a reference to the a, b and c threads it creates.