奶头挺立呻吟高潮av全片,成人试看120秒体验区,性欧美极品v,A片高潮抽搐揉捏奶头视频

java語言

java Runnable接口創建線程詳解

時間:2025-02-15 13:08:59 java語言 我要投稿
  • 相關推薦

java Runnable接口創建線程詳解

  創建一個線程,最簡單的方法是創建一個實現Runnable接口的類。以下是百分網小編搜索整理的關于java Runnable接口創建線程詳解,供參考學習,希望對大家有所幫助!想了解更多相關信息請持續關注我們應屆畢業生考試網!

  為了實現Runnable,一個類只需要執行一個方法調用run(),聲明如下:

  public void run()

  你可以重寫該方法,重要的是理解的run()可以調用其他方法,使用其他類,并聲明變量,就像主線程一樣。

  在創建一個實現Runnable接口的類之后,你可以在類中實例化一個線程對象。

  Thread定義了幾個構造方法,下面的這個是我們經常使用的:

  Thread(Runnable threadOb,String threadName);

  這里,threadOb 是一個實現Runnable 接口的類的實例,并且 threadName指定新線程的名字。

  新線程創建之后,你調用它的start()方法它才會運行。

  void start();

  實例

  下面是一個創建線程并開始讓它執行的實例:

  // 創建一個新的線程

  class NewThread implements Runnable {

  Thread t;

  NewThread() {

  // 創建第二個新線程

  t = new Thread(this, "Demo Thread");

  System.out.println("Child thread: " + t);

  t.start(); // 開始線程

  }

  // 第二個線程入口

  public void run() {

  try {

  for(int i = 5; i > 0; i--) {

  System.out.println("Child Thread: " + i);

  // 暫停線程

  Thread.sleep(50);

  }

  } catch (InterruptedException e) {

  System.out.println("Child interrupted.");

  }

  System.out.println("Exiting child thread.");

  }

  }

  public class ThreadDemo {

  public static void main(String args[]) {

  new NewThread(); // 創建一個新線程

  try {

  for(int i = 5; i > 0; i--) {

  System.out.println("Main Thread: " + i);

  Thread.sleep(100);

  }

  } catch (InterruptedException e) {

  System.out.println("Main thread interrupted.");

  }

  System.out.println("Main thread exiting.");

  }

  }

  編譯以上程序運行結果如下:

  Child thread: Thread[Demo Thread,5,main]

  Main Thread: 5

  Child Thread: 5

  Child Thread: 4

  Main Thread: 4

  Child Thread: 3

  Child Thread: 2

  Main Thread: 3

  Child Thread: 1

  Exiting child thread.

  Main Thread: 2

  Main Thread: 1

  Main thread exiting.

【java Runnable接口創建線程詳解】相關文章:

java Runnable接口如何創建線程09-11

如何創建并運行Java線程10-01

詳解java線程的生命周期09-15

Java創建線程的三種方法07-26

Java 多線程之synchronized關鍵字詳解10-04

java的多線程09-09

java多線程08-31

Java多線程的線程守護例子06-08

Java線程知識筆記09-17

主站蜘蛛池模板: 石景山区| 韩城市| 左云县| 嘉峪关市| 古浪县| 三穗县| 永靖县| 西乡县| 察雅县| 普格县| 遂溪县| 清远市| 温州市| 平定县| 县级市| 陆河县| 武宁县| 崇文区| 呼图壁县| 长乐市| 错那县| 白沙| 洞口县| 齐齐哈尔市| 东兰县| 娱乐| 望都县| 赞皇县| 大宁县| 班戈县| 比如县| 辉县市| 平阳县| 和田市| 仙桃市| 调兵山市| 云林县| 景洪市| 汝阳县| 黑龙江省| 清丰县|