0
public abstract class Event implements Runnable {

  public void run() {
    try {
        Thread.sleep(delayTime);
        action();
    } catch(Exception e) {e.printStackTrace();}
  } 

}

I have this event class above, when I try to start the thread it runs the first command of the thread - Thread.sleep(delayTime); Since the Event class is abstract I want to run some of its child class methods. For example, when I call action(); it should run the action method from the below child class

public class ThermostatNight extends Event {
  public ThermostatNight(long delayTime) {
    super(delayTime);
  }
  public void action() {
    System.out.println(this);
    thermostat = "Night";
  }
  public String toString() {return "Thermostat on night setting";}
}

There are many such child classes, like ThermostatDay, FanOn, FanOff who are very similar as above. What should I do the call action(); after sleep is called from the run() command in Event class ?

Any ideas?

Your help is appreciated!

  • Your code is fine. If you execute the run method of ThermostatDay object, it will invoke the action method of ThermostatDay. Similarly for other objects. The action method of that object will be invoked on which the run method is being executed – Vishal Jun 01 '16 at 03:39
  • Thats not happening, its not printing the System.out.println(this); from the child class action method – A Mahesania Jun 01 '16 at 03:47
  • It should after the delay time has elapsed. Can you show the code how you are executing the run method of the child class? – Vishal Jun 01 '16 at 03:51
  • I would argue that you should call action() first, then sleep, but other than that, it seems fine. – ManoDestra Jun 02 '16 at 20:41
  • I don't understand the motivation behind making something that models an `Event` a `Runnable`. It unnecessarily complicates your efforts. In addition to that you are using methods named `start()`, `ArrayList` of `Thread` objects etc. Why? – Kedar Mhaswade Jun 02 '16 at 20:52

2 Answers2

0

Create and instance variable of type Event and a constructor that takes in an Event as parameter

    public class Event implements Serializable, Runnable {
       private Event childClass;

       public Event(long delayTime, Event childClass) {
         this.delayTime = delayTime;
         this.childClass = childClass;

       }

       public void run() {
          try {
            Thread.sleep(delayTime);
            childClass.action();
          } catch(Exception e) {e.printStackTrace();} 
       }
    } 
tmj010
  • 34
  • 3
  • 1
    one question. why? – zubergu Jun 06 '16 at 10:15
  • Once a class inherit from Event, that class have to give an implementation to the action method and since that class is a child-class of Event you can pass it to the constructor and once you call the action method on the instance variable event in the class it will run the action method in that child-class – tmj010 Jun 06 '16 at 20:35
  • I don´t see the need to use a decorator instead of plain inheritance – fps Jun 12 '16 at 20:06
0

I realized the issue here.

You can see action() is called in try {} below:

  public void run() {
    try {
        Thread.sleep(delayTime);
        action();
    } catch(Exception e) {e.printStackTrace();}
  } 

If you push it out like below, the code should work fine.

public void run() {
    try {
        Thread.sleep(delayTime);
    } catch(Exception e) {e.printStackTrace();}
      action();
  } 
  • No, that's not the reason, unless you're interrupting the thread, which shouldn't be happening in most cases. – fps Jun 12 '16 at 20:00