最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java多線程編程之使用runnable接口創(chuàng)建線程

 更新時間:2014年01月23日 12:09:41   作者:  
實現(xiàn)Runnable接口的類必須使用Thread類的實例才能創(chuàng)建線程,通過Runnable接口創(chuàng)建線程分為以下兩步

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

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

最后通過Thread類的start方法建立線程。
下面的代碼演示了如何使用Runnable接口來創(chuàng)建線程:

package mythread;
public class MyRunnable implements Runnable
{
 public void run()
 {
  System.out.println(Thread.currentThread().getName());
 }
 public static void main(String[] args)
 {
  MyRunnable t1 = new MyRunnable();
  MyRunnable t2 = new MyRunnable();
  Thread thread1 = new Thread(t1, "MyThread1");
  Thread thread2 = new Thread(t2);
  thread2.setName("MyThread2");
  thread1.start();
  thread2.start();
 }
}
[/code]
上面代碼的運行結(jié)果如下:

復制代碼 代碼如下:

MyThread1
MyThread2

相關(guān)文章

最新評論

安新县| 托克逊县| 蒙自县| 北流市| 洪雅县| 错那县| 塘沽区| 抚远县| 亳州市| 辉县市| 宁国市| 土默特右旗| 通化市| 平安县| 庆阳市| 枣阳市| 张家口市| 沙雅县| 丹巴县| 黄冈市| 宁德市| 涿州市| 浪卡子县| 敦煌市| 五大连池市| 新绛县| 湘乡市| 古交市| 金溪县| 出国| 平原县| 措美县| 义乌市| 都兰县| 高唐县| 理塘县| 阿克苏市| 武平县| 湖北省| 遵义市| 正宁县|