日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

线程的常用方法——currentThread方法||在main方法中直接调用run()方法,没有开启新的线程,以在run方法中的当前线程就是main线程||启动子线程,子线程会调用run方法

發布時間:2025/4/16 编程问答 30 豆豆

線程的常用方法——currentThread方法

Thread.currentThread()方法可以獲得當前線程

Java 中的任何一段代碼都是執行在某個線程當中的.

執行當前代碼的線程就是當前線程.

同一段代碼可能被不同的線程執行, 因此當前線程是相對的,

Thread.currentThread()方法的返回值是在代碼實際運行時候的線程對象

SubThread1.java

package com.dym.juc.threadmethod; /* * 定義線程類 * 分別在構造方法和run方法中打印當前線程 * */ public class SubThread1 extends Thread{public SubThread1(){System.out.println("構造方法打印當前線程名稱:"+Thread.currentThread().getName());}@Overridepublic void run() {System.out.println("run方法打印當前線程名稱:"+Thread.currentThread().getName());} }

Test01CurrentThread.java

package com.dym.juc.threadmethod;public class Test01CurrentThread {public static void main(String[] args) {System.out.println("main方法打印當前線程名稱:"+Thread.currentThread().getName());//創建子線程,調用SubThread1()構造方法//在main線程中調用構造方法,所以構造方法中的當前線程就是main線程SubThread1 t1 = new SubThread1();//啟動子線程,子線程會調用run方法//t1.start(); //run方法打印當前線程名稱:Thread-0//在main方法中直接調用run()方法,沒有開啟新的線程//所以在run方法中的當前線程就是main線程t1.run(); //run方法打印當前線程名稱:main} }

總結

以上是生活随笔為你收集整理的线程的常用方法——currentThread方法||在main方法中直接调用run()方法,没有开启新的线程,以在run方法中的当前线程就是main线程||启动子线程,子线程会调用run方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。