創建線程的兩個方式

方式一:

 

package com.Thread1;
public class myRunable implements Runnable {
	public int count;

	@Override
	public void run() {
		while (count < 100) {
			count++;
			System.out.println("count:" + count + "由"
					+ Thread.currentThread().getName() + "創建");
		}
	}
}

 

 

package com.Thread1;
public class myRunableTest {
	public static void main(String[] args) {
		Thread t1 = new Thread(new myRunable(), "線程1");
		t1.start();
		Thread t2 = new Thread(new myRunable(), "線程2");
		t2.start();
	}
}

 

 

方式二:

 

package com.Thread1;
public class MyThread1 extends Thread {
	public int count;

	@Override
	public void run() {
		while (count <100) {
			count++;
			System.out.println("count:"+count+"由"+Thread.currentThread().getName()+"創建");
		}
	}
}

 

 

package com.Thread1;
public class MyThread1Test {
	public static void main(String[] args) {
		// MyThread1 mt1 = new MyThread1();
		// Thread t = new Thread(mt1);
		// t.start();
		Thread t1 = new MyThread1();
		t1.setName("線程1");
		t1.start();
		Thread t2 = new MyThread1();
		t2.setName("線程2");
		t2.start();
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章