日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
初學(xué)Java多線程:使用Runnable接口創(chuàng)建線程

實現(xiàn)Runnable接口的類必須使用Thread類的實例才能創(chuàng)建線程。通過Runnable接口創(chuàng)建線程分為兩步:

1. 將實現(xiàn)Runnable接口的類實例化。

2.     建立一個Thread對象,并將第一步實例化后的對象作為參數(shù)傳入Thread類的構(gòu)造方法。

   最后通過Thread類的start方法建立線程。

下面的代碼演示了如何使用Runnable接口來創(chuàng)建線程:

 
 
 
  1. package mythread;  
  2.  
  3. public class MyRunnable implements Runnable  
  4. {  
  5.     public void run()  
  6.     {  
  7.         System.out.println(Thread.currentThread().getName());  
  8.     }  
  9.     public static void main(String[] args)  
  10.     {  
  11.         MyRunnable t1 = new MyRunnable();  
  12.         MyRunnable t2 = new MyRunnable();  
  13.         Thread thread1 = new Thread(t1, "MyThread1");  
  14.         Thread thread2 = new Thread(t2);  
  15.         thread2.setName("MyThread2");  
  16.         thread1.start();  
  17.         thread2.start();  
  18.     }  
  19. }  

上面代碼的運行結(jié)果如下:

MyThread1
MyThread2

舉例Java多線程的學(xué)習(xí)又更近一步了。

【編輯推薦】

  1. 初學(xué)Java多線程:用Thread類創(chuàng)建線程
  2. 初學(xué)Java多線程:線程簡介
  3. Java多線程編程的常見陷阱
  4. Java多線程的相關(guān)機(jī)制
  5. Java多線程學(xué)習(xí)總結(jié)(二)

網(wǎng)站名稱:初學(xué)Java多線程:使用Runnable接口創(chuàng)建線程
文章路徑:http://www.5511xx.com/article/coeddch.html