国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

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

2019-11-26 15:44:10
字體:
供稿:網(wǎng)友

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

2.建立一個(gè)Thread對(duì)象,并將第一步實(shí)例化后的對(duì)象作為參數(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]
上面代碼的運(yùn)行結(jié)果如下:

復(fù)制代碼 代碼如下:

MyThread1
MyThread2

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 贵港市| 肇庆市| 景宁| 榆中县| 克拉玛依市| 海南省| 玛多县| 永康市| 绍兴县| 犍为县| 定远县| 札达县| 清远市| 丰镇市| 剑阁县| 望谟县| 新巴尔虎右旗| 行唐县| 韩城市| 卢龙县| 长阳| 达州市| 定襄县| 和林格尔县| 荔浦县| 凌云县| 同江市| 澎湖县| 鹤山市| 无锡市| 锦屏县| 拜城县| 呼玛县| 东明县| 宣威市| 南漳县| 慈溪市| 沧源| 渑池县| 新巴尔虎左旗| 安阳市|