I have to create a
thread for each new object created in a restart class. I have the code compiling, but after it hits t.start() it just ends instead of going on to the run method. Could anyone tell me why it is not going to run()? The thread is in this class.
I am a rookie at
Java and this is based on supplied code that I have modified for an assignment. I am not looking for anyone to do the coding for me, just some advice. I would really appreciate any help at all.
Here is the code for creating the threads.
import java.lang.ThreadGroup;
import java.lang.Thread;
abstract class Event extends Thread {
private long delay;
private ThreadGroup tg = new ThreadGroup("eventGroup");
private Thread t;
public Event(long delayTime, int i) {
String s = Integer.toString(i);
t = new Thread(tg, s);
delay = delayTime;
t.start();
}
public void run() {
try {
t.sleep(delay);
} catch(InterruptedException e) {
System.out.println(
"InterruptedException = " + e);
}
action();
if(description().length()>0)
System.out.println(description());
System.out.println(t.getThreadGroup());
}
abstract public void action();
abstract public String description();
}
This has the classes for the objects that have to be created.
class GreenhouseControls {
private boolean light = false;
private boolean
water = false;
private String thermostat = "Day";
private int i;
private class LightOn extends Event {
public LightOn(long delayTime) {
super(delayTime, i);
i++;
}
public void action() {
light = true;
}
public String description() {
return "Light is on";
}
}
private class LightOff extends Event {
public LightOff(long delayTime) {
super(delayTime, i);
i++;
}
public void action() {
light = false;
}
public String description() {
return "Light is off";
}
}
private class WaterOn extends Event {
public WaterOn(long delayTime) {
super(delayTime, i);
i++;
}
public void action() {
water = true;
}
public String description() {
return "Greenhouse water is on";
}
}
private class WaterOff extends Event {
public WaterOff(long delayTime) {
super(delayTime, i);
i++;
}
public void action() {
water = false;
}
public String description() {
return "Greenhouse water is off";
}
}
private class ThermostatNight extends Event {
public ThermostatNight(long delayTime) {
super(delayTime, i);
i++;
}
public void action() {
thermostat = "Night";
}
public String description() {
return "Thermostat on night setting";
}
}
private class ThermostatDay extends Event {
public ThermostatDay(long delayTime) {
super(delayTime, i);
i++;
}
public void action() {
thermostat = "Day";
}
public String description() {
return "Thermostat on day setting";
}
}
private class Bell extends Event {
public Bell(long delayTime) {
super(delayTime, i);
i++;
}
public void action() {
System.out.println("Bing!");
}
public String description() {
return "";
}
}
private class Finish extends Event {
public Finish(long delayTime) {
super(delayTime, i);
i++;
}
public void action() {
System.out.println("Restarting System");
}
public String description() {
return "Terminating";
}
}
class Restart extends Event {
public Restart(long delayTime) {
super(delayTime, i);
i++;
}
public void action() {
new Bell(0);
new ThermostatNight(100);
new LightOn(200);
new LightOff(300);
new WaterOn(800);
new WaterOff(900);
new ThermostatDay(1000);
new Finish(2000);
}
public String description() {
return "";
}
}
}
public class Tme3Question1 {
public static void main(String[] args) {
GreenhouseControls gc = new GreenhouseControls();
gc.new Restart(1000);
}
}/* Output:
Bing!
Thermostat on night setting
Light is on
Light is off
Greenhouse water is on
Greenhouse water is off
Thermostat on day setting
Restarting system
Terminating
*///:~