Thread Group

Hi Team,

I have a question regarding thread groups
I have a class called Event that looks like this (below). It performs an action based on some time.

If I wanted to create a threadgroup and then use it to set the prority, what is the best way to modify the code below to do that.

I assume that right in that Event class we need to somehow create a method which calls setPriority() and pass it an integer to set the priority.
Thanks.

===============
abstract class Event extends Thread {
private long delay;

public Event(long delayTime) {
delay = delayTime;
start();
}

public void run() {
try {
sleep(delay);
} catch(InterruptedException e) {
System.out.println("InterruptedException = " + e);
}
action();
System.out.println(this);
}
abstract public void action();
}
===============
pauldhadiallaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

petmagdyCommented:
change u constructor to be:

    public Event(long delayTime, ThreadGroup group) {
        super(group, "ThreadName");
        delay = delayTime;
        start();
        }

in ur Thread initiator or main method will look like this:

    public static void main(String[] args) {
        ThreadGroup group = new ThreadGroup("ThreadGroupName");
        System.out.println(group.getMaxPriority()); // Maximum periorityis 10
        group.setMaxPriority(8);
        Event event = new UrEventExtendingClass(10000, group);
    }

0
pauldhadiallaAuthor Commented:
petmagdy,

My original class that extends event is like this:

private class WW extends Event {
public WW (long delayTime) {
super(delayTime);
}
public void action() {
// Put hardware control code here
w = false;
}
public String toString() {
return "w is off";
}
}


There is a class that calls that WW class above like this:
class Begin extends Event {
public Begin(long delayTime) {
super(delayTime);
}

The main looks like this:

public class StartThread {
public static void main(String[] args) {
Controller gcc = new Controller();
gcc.new Restart(1000);

So.....

In all of these , my classes are expectig only delay time, so in the event class above, as you suggested, I create (long delayTime, ThreadGroup group)
So that means I will have to modify main() to say gcc.new.Restart(1000, group) ?

Then the Begin Class will say:
public Begin(long delayTime, ThreadGroup group)?

and the WW Class will say:
public WW (long delayTime, ThreadGroup group)?

Not sure if that is right. The Event - is it expecting a string with the name of the group then.

Thank you.

0
petmagdyCommented:
>> So that means I will have to modify main() to say gcc.new.Restart(1000, group) ?
I don't know what Controller.Restart(1000) do, but if it the one starts ur threads then u can initialize the Thread Group inside the Restart() or inside the main method and pass it to Restart, ur choice

>>public Begin(long delayTime, ThreadGroup group)?
yes

>>public WW (long delayTime, ThreadGroup group)?
yes

>>Not sure if that is right. The Event - is it expecting a string with the name of the group then.
ur Decision, u can either keep the Thread name fixed or pass it to the construtor of Event. The necessity of the name is their is no constructor for Tread take only ThreadGroup

hope I answered u tell me if u need any more clarification regarding ThreadGroup
Good Luck
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

petmagdyCommented:
another Idea to handle ThreadGoup is to handle it using SingleTone and factory model like this
Create a class called MyThreadGroupFactory like this:

public class MyThreadGroupFactory {

    private ThreadGroup threadGroup = null;
    private static MyThreadGroupFactory instance = new MyThreadGroupFactory();
   
    private MyThreadGroupFactory()
    {
        threadGroup = new ThreadGroup("ThreadGroupName");
        threadGroup.setMaxPriority(8); // or any other value
    }
   
    public static ThreadGroup getThreadGroup()
    {
        return instance.threadGroup;
    }
}

and just in Event constructor:

    public Event(long delayTime) {
        super(MyThreadGroupFactory.getThreadGroup() ,"EventThread");
        delay = delayTime;
        start();
        }

and every thing returns to normal without modification
Enjoy :)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pauldhadiallaAuthor Commented:
Thanks petmagdy ! This helps alot.
Cheers my friend.
0
petmagdyCommented:
u r welcome ;-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.