国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 鄂州市| 梨树县| 四会市| 湟源县| 大洼县| 柘荣县| 乳山市| 洮南市| 通河县| 时尚| 平塘县| 长汀县| 嫩江县| 安康市| 伊金霍洛旗| 隆安县| 铜山县| 海原县| 隆回县| 日照市| 胶州市| 庐江县| 莱西市| 永嘉县| 萍乡市| 宣武区| 新田县| 陆河县| 望城县| 铅山县| 台湾省| 赣榆县| 卓尼县| 灯塔市| 达孜县| 太康县| 贺州市| 彭州市| 库尔勒市| 南汇区| 万州区|