每当运行一个 Java 程序时,主线程会自动创建。这个线程负责 Java 程序的执行。Java 运行时会寻找 main
方法来执行,并基于此创建一个主线程。如果我们创建了多个线程,那么所有的子线程都将由主线程派生出来。主线程是第一个被创建的线程,通常也是最后一个结束的线程,并且它用来执行关闭任务。
如何控制主线程?
主线程是由 JVM 在程序启动时自动创建的。但你可以通过不同的线程方法和技术来控制主线程。
以下是一些用于控制主线程的方法:
主线程示例
在这个例子中,我们展示了一个简单的单线程程序,其中我们没有声明任何线程,并检查程序执行中的线程名称。
package com.tutorialspoint;
public class TestThread {
public void printName() {
System.out.println("Thread Name: " + Thread.currentThread().getName());
System.out.println("Thread Priority: " +Thread.currentThread().getPriority());
}
public static void main(String args[]) {
TestThread thread = new TestThread();
thread.printName();
}
}
输出
Thread Name: main
Thread Priority: 5
更多主线程示例
示例
在这个例子中,我们创建了一个 ThreadDemo
类,该类继承了 Thread
类。我们没有向线程传递任何名字,它将打印系统分配给线程的默认名字。在 main
方法中,我们创建了两个线程。在输出中,你可以看到当前线程的名字被打印为 main
,而线程是通过构造函数调用创建的。
package com.tutorialspoint;
class ThreadDemo extends Thread {
ThreadDemo( ) {
System.out.println("Thread: " + Thread.currentThread().getName() + ", " + "State: New");
}
public void run() {
System.out.println("Thread: " + Thread.currentThread().getName() + ", " + "State: Running");
for(int i = 4; i > 0; i--) {
System.out.println("Thread: " + Thread.currentThread().getName() + ", " + i);
}
System.out.println("Thread: " + Thread.currentThread().getName() + ", " + "State: Dead");
}
public void start () {
System.out.println("Thread: " + Thread.currentThread().getName() + ", " + "State: Start");
super.start();
}
}
public class TestThread {
public static void main(String args[]) {
ThreadDemo thread1 = new ThreadDemo();
ThreadDemo thread2 = new ThreadDemo();
thread1.start();
thread2.start();
}
}
输出
Thread: main, State: New
Thread: main, State: New
Thread: main, State: Start
Thread: main, State: Start
Thread: Thread-0, State: Running
Thread: Thread-0, 4
Thread: Thread-0, 3
Thread: Thread-1, State: Running
Thread: Thread-1, 4
Thread: Thread-0, 2
Thread: Thread-1, 3
Thread: Thread-0, 1
Thread: Thread-1, 2
Thread: Thread-0, State: Dead
Thread: Thread-1, 1
Thread: Thread-1, State: Dead
示例 2
在这个例子中,我们创建了一个 ThreadDemo
类,该类继承了 Thread
类。我们没有向线程传递任何名字,它将打印系统分配给线程的默认名字。在 main
方法中,我们创建了两个线程。在输出中,你可以看到当前线程的名字被打印为 main
,而线程是通过构造函数调用创建的。在 main
方法的末尾,我们打印了主线程的状态。
package com.tutorialspoint;
class ThreadDemo extends Thread {
ThreadDemo( ) {
System.out.println("Thread: " + Thread.currentThread().getName() + ", " + "State: New");
}
public void run() {
System.out.println("Thread: " + Thread.currentThread().getName() + ", " + "State: Running");
for(int i = 4; i > 0; i--) {
System.out.println("Thread: " + Thread.currentThread().getName() + ", " + i);
}
System.out.println("Thread: " + Thread.currentThread().getName() + ", " + "State: Dead");
}
public void start () {
System.out.println("Thread: " + Thread.currentThread().getName() + ", " + "State: Start");
super.start();
}
}
public class TestThread {
public static void main(String args[]) {
ThreadDemo thread1 = new ThreadDemo();
ThreadDemo thread2 = new ThreadDemo();
thread1.start();
thread2.start();
System.out.println("Thread: " + Thread.currentThread().getName() + ", " + "State: Dead");
}
}
输出
Thread: main, State: New
Thread: main, State: New
Thread: main, State: Start
Thread: main, State: Start
Thread: Thread-0, State: Running
Thread: main, State: Dead
Thread: Thread-1, State: Running
Thread: Thread-0, 4
Thread: Thread-1, 4
Thread: Thread-1, 3
Thread: Thread-1, 2
Thread: Thread-1, 1
Thread: Thread-1, State: Dead
Thread: Thread-0, 3
Thread: Thread-0, 2
Thread: Thread-0, 1
Thread: Thread-0, State: Dead
在上述输出中,你可以看到主线程在早期阶段就已经结束,但其他线程仍在运行并完成了它们的执行。