Example: Starting a thread using a Java program
This program shows how to start a thread in a Java™ program.
Note: By using the code examples, you agree to the terms of the Code license and disclaimer information.
/*
FileName: ATEST11.java
The output of this example is as follows:
Entered the testcase
Create a thread with parameters
Start the thread
Wait for the thread to complete
Thread Parameters: 42 is the answer to "Life, the Universe and Everything"
Testcase completed
*/
import java.lang.*;
public class ATEST11 {
static class theThread extends Thread {
int threadParm1;
String threadParm2;
public theThread(int i, String s) {
threadParm1 = i;
threadParm2 = s;
}
public void run() {
System.out.print("Thread Parameters: " + String.valueOf(threadParm1) +
" is the answer to \"" + threadParm2 + "\"\n");
}
}
public static void main(String argv[]) {
System.out.print("Entered the testcase\n");
System.out.print("Create a thread with parameters\n");
theThread t = new theThread(42, "Life, the Universe and Everything");
System.out.print("Start the thread\n");
t.start();
System.out.print("Wait for the thread to complete\n");
try {
t.join();
}
catch (InterruptedException e) {
System.out.print("Join interrupted\n");
}
System.out.print("Testcase completed\nj");
System.exit(0);
}
}