Java Reference
In-Depth Information
4. Compile the class in the DOS command window:
javac MsgThreadRunnable.java
5. Edit the HelloWorld.java source file in your java4cobol directory with a
text editor. Delete the code from the main() method but not the main()
method itself.
6. Next, add the following code to create and then use MsgThread and Msg-
ThreadRunnable .
import java.util.*;
//
//
// HelloWorld
//
//
public class HelloWorld
{
public static void main(String args[]) {
// Create an object that inherits from Thread,
// and then start that Thread.
MsgThread run1 = new MsgThread ();
run1.start();
// Create an object that implements Runnable,
// and then start the execution of that class.
MsgThreadRunnable msg1 = new MsgThreadRunnable ();
Thread run2 = new Thread (msg1);
run2.start();
// Wait a few milliseconds, then stop those threads.
try {
Thread.currentThread().sleep(100);
}
catch (InterruptedException e) {
}
// Use the Thread object in this case.
run1.stopThread();
Search WWH ::




Custom Search