Link to home
Start Free TrialLog in
Avatar of pauldhadialla
pauldhadialla

asked on

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();
}
===============
Avatar of petmagdy
petmagdy
Flag of Canada image

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);
    }

Avatar of pauldhadialla
pauldhadialla

ASKER

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.

>> 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
ASKER CERTIFIED SOLUTION
Avatar of petmagdy
petmagdy
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks petmagdy ! This helps alot.
Cheers my friend.
u r welcome ;-)