Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 220
  • Last Modified:

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();
}
===============
0
pauldhadialla
Asked:
pauldhadialla
  • 4
  • 2
1 Solution
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
pauldhadiallaAuthor Commented:
Thanks petmagdy ! This helps alot.
Cheers my friend.
0
 
petmagdyCommented:
u r welcome ;-)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now