import java.util.Date;
/**
* @author joanna
*
* Demonstrate making a thread per class.
* This gets called by GreetingThreadRunner
*/
public class GreetingThread extends Thread {
private String greeting;
public GreetingThread (String aGreeting) {
greeting = aGreeting;
}
private static final int REPETITIONS = 10;
private static final int DELAY = 1000;
public void run () {
try {
for (int i=1; i<= REPETITIONS; i++) {
Date now = new Date ();
System.out.println(now+ ": " + greeting);
sleep (DELAY);
}
} catch (InterruptedException e) {
System.out.println("Goodbye! from: " + e);
} // catch
} // run
}
/**
* @author joanna
*
* This just runs GreetingThread -- how silly!
*/
public class GreetingThreadRunner {
public static void main(String[] args) {
GreetingThread t1 = new GreetingThread("Hi There!");
GreetingThread t2 = new GreetingThread("Ni hao!");
GreetingThread t3 = new GreetingThread("Hullo, jolly good, what?");
t1.start();
t2.start();
t3.start();
}
}
import java.util.Date;
/**
* @author joanna
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class GreetRunnable implements Runnable {
private String greeting;
private Thread greetThread; // must have a thread around the place
public GreetRunnable (String aGreeting) {
greeting = aGreeting;
greetThread = new Thread(GreetRunnable.this); // pass own class to Thread constuctor
}
private static final int REPETITIONS = 10;
private static final int DELAY = 1000; // one second
public void run () {
try {
for (int i=1; i<= REPETITIONS; i++) {
Date now = new Date ();
System.out.println(now+ ": " + greeting);
Thread.sleep (DELAY); //now need to reference Thread for this!
}
} catch (InterruptedException e) {
System.out.println("Goodbye! from: " + e);
} // catch
} // run
/* and now we need a start...*/
public void start () {
greetThread.start();
}
} // class GreetRunnable
public class RunRunnableGreeting {
public static void main(String[] args) {
GreetRunnable t1 = new GreetRunnable("Hi There!");
GreetRunnable t2 = new GreetRunnable("Ni hao!");
GreetRunnable t3 = new GreetRunnable("Hullo, jolly good, what?");
t1.start();
t2.start();
t3.start();
}
}
import java.util.Date;
/**
* @author joanna
*
* OK, now do it with an inner class
*/
public class GreetInner { // doesn't implement or extend anything...
private String greeting;
private Thread greetThread;
private static final int REPETITIONS = 10;
private static final int DELAY = 1000; // one second
public GreetInner (String aGreeting) {
greeting = aGreeting;
greetThread = new Thread() { // inner class! extends Thread...
public void run () { // the new def. of run is just like the previous ones
try {
for (int i=1; i<= REPETITIONS; i++) {
Date now = new Date ();
System.out.println(now+ ": " + greeting);
Thread.sleep (DELAY); //now need to reference Thread for this!
}
} catch (InterruptedException e) {
System.out.println("Goodbye! from: " + e);
} // catch
} // run()
}; // our inner class def! (note it needs a semicolon)
/* we have to start this in here, since it isn't known outside the scope of the method*/
greetThread.start();
} // our constructor
}
public class RunGreetInner {
public static void main(String[] args) {
GreetInner t1 = new GreetInner("Hi There!");
GreetInner t2 = new GreetInner("Ni hao!");
GreetInner t3 = new GreetInner("Hullo, jolly good, what?");
}
}