• Post Reply Bookmark Topic Watch Topic
  • New Topic

ThreadGroup  RSS feed

 
Kathy Thiyagar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to get this ThreadGroup working. I have an abstract class and two innerclasses, ClassA and ClassB which extends this abstract class. I created the abstract class that implements the Runnable interface. In ClassA and ClassB, I created the Threads. I have two question:

Q1) In my abstract class, why my run method is not called?
Q2) How do I get the ThreadGroups for these two threads running with the ThreadCount?

I would really appreciate as I am learning Threads now need some guidance. I have copied the code below.



Thanks in advance,

Kathy
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Too difficult a question for us here on "beginning". Moving thread.
 
Manish Singh
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) Because you are not calling start method on the thread object
 
Karthik Shiraly
Bartender
Posts: 1210
25
Android C++ Java Linux PHP Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kathy,

For Q1: BaseClass.run() will never be called, because both the concrete subclasses - ClassA and ClassB - override it with their own definitions, and neither call super.run().
Moreover, the start() method here is your own start() method and it does not actually create an OS thread. In fact, everything here will run in just 1 thread, that is the main thread. No other OS threads are being created here at all!

For Q2: If you could briefly describe what you're trying to do in this program, it would be easier to help.

Couldn't help noticing that both ClassA.run() and ClassB.run() have these 2 lines:

Not sure what you're trying to do, but what will happen is that a Thread *object* (just an object, not an OS thread) is created.
And "t.sleep()" will NOT put thread t to sleep.
sleep() always puts only the current active OS thread to sleep.
sleep() is a static method of Thread, so calling it on a Thread object, though syntactically valid, will mislead a reader. Please use "Thread.sleep();".
The Thread object t being created here is unnecessary because it's never getting used or started.
 
Kathy Thiyagar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Karthik,

Thanks for the quick reply. I understand the first one. For the second question, what I am trying to do is, for each thread created from ClassA or ClassB, it should create its own timings based on the delayTime and then I want to put them into a ThreadGroup object and want to set the maimum priority when everytime it is created. This is the part I need some help with. Could you please help me with this?

I will use Thread.sleep() as you suggested.

Note: Noticed that you are a SCJP 6. WOW.

Thanks Karthik,

Kathy
 
Karthik Shiraly
Bartender
Posts: 1210
25
Android C++ Java Linux PHP Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kathy,

Sure! Suggest you do this step by step, and get each step working. Since you're learning, I won't spoil the fun with all details but will just give brief steps:

- First, make ClassA and ClassB run as separate threads, without involving a ThreadGroup at all. Right now, there are no threads created. Briefly, create in Restart class two Thread objects with ClassA and ClassB as their Runnables, and start them. Remove the custom start() methods (or atleast don't call them for now, because they'll confuse).

- Then, modify the thread creation statements to include a ThreadGroup object.

- Finally, set the max priority.

Feel free to ask if you run into problems.

Cheers
Karthik
 
Kathy Thiyagar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Karthik,

Thank you for the support. I have modified my Restart class and included the following. Please let me know if I am in the correct path. Now, if I want to get the ThreadCount, I just have to get the handle on the parent thread group and loop through? Please advise.



Thanks Karthik,

Kathy
 
Karthik Shiraly
Bartender
Posts: 1210
25
Android C++ Java Linux PHP Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kathy,

Yes, perfect!
I don't know what the Event class here does, but I assume from code that it somehow schedules to call action() method. Is this right?
For getting just the thread count, ThreadGroup has a activeCount() method. No need to loop over the thread group, unless you want to call some method on each thread.

Cheers
Karthik
 
Kathy Thiyagar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much Karthik. Yes, you are correct. it does call the action methods. I was able to get the active thread counts using the activeCount() method. Great.

Once again, thank you and appreciate all of your help on this.

Regards,

Kathy
 
Karthik Shiraly
Bartender
Posts: 1210
25
Android C++ Java Linux PHP Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!