【JUC】1-Java執行緒的啟動

不会java的菜鸟程序员發表於2024-06-05

以Thread建立執行緒為例:

1 Thread thread = new Thread() {
2             @Override
3             public void run() {
4                 log.info("create and start a thread through creating a Thread Object");
5             }
6         };
7thread. start();

跟蹤start()方法的原始碼

 1 public synchronized void start() {
 2         /**
 3          * This method is not invoked for the main method thread or "system"
 4          * group threads created/set up by the VM. Any new functionality added
 5          * to this method in the future may have to also be added to the VM.
 6          *
 7          * A zero status value corresponds to state "NEW".
 8          */
 9         if (threadStatus != 0)
10             throw new IllegalThreadStateException();
11 
12         /* Notify the group that this thread is about to be started
13          * so that it can be added to the group's list of threads
14          * and the group's unstarted count can be decremented. */
15         group.add(this);
16 
17         boolean started = false;
18         try {
19             start0();
20             started = true;
21         } finally {
22             try {
23                 if (!started) {
24                     group.threadStartFailed(this);
25                 }
26             } catch (Throwable ignore) {
27                 /* do nothing. If start0 threw a Throwable then
28                   it will be passed up the call stack */
29             }
30         }
31     }

發現,實際呼叫的是start0()方法,而這個方法是一個本地方法;

1 private native void start0();

想要了解這個方法的內部實現,就必須去檢視jdk的原始碼了,是由C++進行實現的

1、Thread類啟動執行緒

 1 /**
 2      * 透過建立Thread物件建立執行緒
 3      */
 4     private static void testCreateThread() {
 5         Thread thread = new Thread() {
 6             @Override
 7             public void run() {
 8                 log.info("create and start a thread through creating a Thread Object");
 9             }
10         };
11         thread. Start();
12     }

2、實現Runable介面

 1 /**
 2  * 建立和執行執行緒
 3  */
 4 @Slf4j(topic = "c.MyThread")
 5 public class MyThread02 {
 6     public static void main(String[] args) {
 7         testCreateThread();
 8     }
 9 
10     /**
11      * 透過實現Runnable介面建立執行緒
12      */
13     private static void testCreateThread() {
14         Task task = new Task();
15         task.run();
16     }
17 }
18 @Slf4j(topic = "c.Runnable")
19 class Task implements Runnable {
20     @Override
21     public void run() {
22         log.info("create and start a thread through implements Runnable interface");
23     }
24 }

3、FutureTask介面

 1     public static void main(String[] args) throws ExecutionException, InterruptedException {
 2         // 建立任務物件
 3         FutureTask<Integer> futureTask = new FutureTask(() ->{
 4             log.debug("hello");
 5             Thread.sleep(2000);
 6             return 100;
 7         });
 8         // 建立執行緒
 9         new Thread(futureTask,"t3").start();
10 
11         // 主執行緒阻塞,同步等待futureTask執行完畢的結果
12         Integer result = futureTask.get();
13         log.info("{}",result);
14     }

相關文章