Java中的主線程


概覽

前段時間有同事提到了主線程這個名詞,但當時我們說的主線程是指Java Web程序中每一個請求進來時處理邏輯的線程。當時感覺這個描述很奇怪,所以就來研究下這個主線程的確切語義。

Java提供了內置的多線程編程支持,多線程包括兩個或多個可並發執行的部分,每一部分叫做線程,每個線程定義了單獨的執行部分。

主線程

當一個Java程序啟動的時候,會有一個線程立即開始運行,這個線程通常被我們叫做程序中的主線程,因為它是在我們程序開始的時候就被執行的線程。

  • 子線程都從該線程中被孵化
  • 通常它都是最后一個執行結束的線程,因為它會執行各種的關閉操作。

流程圖:
Java主線程流程圖

怎么來控制主線程

主線程在程序啟動時會被自動創建,為了能夠控制它我們必須獲取到它的引用,我們可以在當前類中調用currentThread()方法來獲取到,該方法返回一個當前線程的引用。

主線程默認的優先級是5,所有子線程都將繼承它的優先級。

public class Test extends Thread {
    public static void main(String[] args) {
        // getting reference to Main thread
        Thread t = Thread.currentThread();

        // getting name of Main thread
        System.out.println("Current thread: " + t.getName());

        // changing the name of Main thread
        t.setName("Geeks");
        System.out.println("After name change: " + t.getName());

        // getting priority of Main thread
        System.out.println("Main thread priority: " + t.getPriority());

        // setting priority of Main thread to MAX(10)
        t.setPriority(MAX_PRIORITY);

        System.out.println("Main thread new priority: " + t.getPriority());

        for (int i = 0; i < 5; i++) {
            System.out.println("Main thread");
        }

        // Main thread creating a child thread
        ChildThread ct = new ChildThread();

        // getting priority of child thread
        // which will be inherited from Main thread
        // as it is created by Main thread
        System.out.println("Child thread priority: " + ct.getPriority());

        // setting priority of Main thread to MIN(1)
        ct.setPriority(MIN_PRIORITY);

        System.out.println("Child thread new priority: " + ct.getPriority());

        // starting child thread
        ct.start();
    }
}

// Child Thread class
class ChildThread extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 5; i++) {
            System.out.println("Child thread");
        }
    }
}

看下輸出:

Current thread: main
After name change: Geeks
Main thread priority: 5
Main thread new priority: 10
Main thread
Main thread
Main thread
Main thread
Main thread
Child thread priority: 10
Child thread new priority: 1
Child thread
Child thread
Child thread
Child thread
Child thread

主線程和main()函數的關系

對每個程序而言,主線程都是被JVM創建的,從JDK6開始,main()方法在Java程序中是強制使用的。

主線程中的死鎖(單個線程)

我們可以使用主線程創建一個死鎖:

public class Test {
    public static void main(String[] args) {
        try {
            System.out.println("Entering into Deadlock");
            Thread.currentThread().join();
            // the following statement will never execute 
            System.out.println("This statement will never execute");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
} 

看下輸出:

Entering into Deadlock

語句Thread.currentThread().join()會告訴主線程一直等待這個線程(也就是等它自己)運行結束,所以我們就有了死鎖。

關於Java中的死鎖和活鎖可以參考這篇文章


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM