Java Reference
In-Depth Information
super ();
}
public void stopThread () {
timeToStop = true;
}
public void run () {
while (!timeToStop) {
// Create a loop that runs till timeToStop.
System.out.println ("Inside thread " +
Thread.currentThread().getName());
}
System.out.println ("This thread is stopping " +
Thread.currentThread().getName());
}
}
2. Compile the class in the DOS command window:
javac MsgThread.java
3. Using a text editor, create a file named MsgThreadRunnable.java. Add
these lines:
public class MsgThreadRunnable
implements Runnable {
private volatile Boolean timeToStop = false;
public void stopThread () {
timeToStop = true;
}
public void run () {
// Create a loop that runs till timeToStop.
while (!timeToStop) {
System.out.println ("Inside thread " +
Thread.currentThread().getName());
}
System.out.println ("This thread is stopping " +
Thread.currentThread().getName());
}
}
Search WWH ::




Custom Search