• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

thread woes

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
*///:~
 
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I didn't spend whole lot of time to look at the code. I looked at where you call the start method and I see that the parameter to Thread constructor is threadgroup and String object. If you take a look at JDK doc for this method here is what it says



And you basically have "null" runnable object wrapped around the thread you constructed.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is with this line: t = new Thread(tg, s); in the constructor.
It will create and allocate a new Thread but when you call start() method it will call the run method of 'Thread' class and not of your class.

Instead you should call super(tg,s) (it will call super class constructor ie. Thread class in your case). Now calling this.start() method will create and schedule a new thread and it will call your class's run method.

Pratik
 
Roses are red, violets are blue. Some poems rhyme and some don't. And some poems are a tiny ad.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic