论坛首页 入门技术论坛

线程随笔-创建线程的基本方法

浏览 1318 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-09-03  

The method One:

 

public class testThreadNormal {

	public static void main(String[] args) {
		Runner r1 = new Runner();
		Thread t1 = new Thread(r1);
		t1.start();
		for (int i = 1; i < 50; i++) {
			System.out.println("times of calling main thread" + i);
		}
	}
}

class Runner implements Runnable {

	public void run() {
		// TODO Auto-generated method stub
		for (int i = 1; i < 50; i++) {
			System.out.println("times of calling thread of Runner:" + i);
		}
	}
}

 

 

 The result of method One:

 

times of calling thread of Runner:1
times of calling main thread1
times of calling thread of Runner:2
times of calling main thread2
times of calling thread of Runner:3
times of calling main thread3
times of calling thread of Runner:4
times of calling main thread4
times of calling thread of Runner:5
times of calling main thread5
times of calling thread of Runner:6
times of calling main thread6
times of calling thread of Runner:7
times of calling main thread7
times of calling thread of Runner:8
times of calling main thread8
times of calling thread of Runner:9
times of calling main thread9
times of calling thread of Runner:10
times of calling main thread10
times of calling thread of Runner:11
times of calling main thread11
times of calling thread of Runner:12
times of calling main thread12
times of calling thread of Runner:13
times of calling main thread13
times of calling thread of Runner:14
times of calling main thread14
times of calling thread of Runner:15
times of calling main thread15
times of calling thread of Runner:16
times of calling main thread16
times of calling thread of Runner:17
times of calling main thread17
times of calling thread of Runner:18
times of calling main thread18
times of calling thread of Runner:19
times of calling main thread19
times of calling thread of Runner:20
times of calling thread of Runner:21
times of calling thread of Runner:22
times of calling thread of Runner:23
times of calling thread of Runner:24
times of calling thread of Runner:25
times of calling thread of Runner:26
times of calling thread of Runner:27
times of calling thread of Runner:28
times of calling main thread20
times of calling thread of Runner:29
times of calling main thread21
times of calling thread of Runner:30
times of calling main thread22
times of calling thread of Runner:31
times of calling main thread23
times of calling main thread24
times of calling main thread25
times of calling main thread26
times of calling main thread27
times of calling main thread28
times of calling main thread29
times of calling main thread30
times of calling thread of Runner:32
times of calling main thread31
times of calling thread of Runner:33
times of calling main thread32
times of calling thread of Runner:34
times of calling thread of Runner:35
times of calling thread of Runner:36
times of calling thread of Runner:37
times of calling thread of Runner:38
times of calling thread of Runner:39
times of calling main thread33
times of calling thread of Runner:40
times of calling main thread34
times of calling thread of Runner:41
times of calling main thread35
times of calling thread of Runner:42
times of calling main thread36
times of calling thread of Runner:43
times of calling main thread37
times of calling thread of Runner:44
times of calling main thread38
times of calling thread of Runner:45
times of calling main thread39
times of calling thread of Runner:46
times of calling main thread40
times of calling thread of Runner:47
times of calling main thread41
times of calling thread of Runner:48
times of calling main thread42
times of calling thread of Runner:49
times of calling main thread43
times of calling main thread44
times of calling main thread45
times of calling main thread46
times of calling main thread47
times of calling main thread48
times of calling main thread49

 

Pay Attention: a wrong method of constructing a new thread

public class testThread1 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Runner1 r1 = new Runner1();
		r1.run();
		for (int i = 1; i < 20; i++) {
			System.out.println("times of calling main thread" + i);
		}
	}

}

class Runner1 implements Runnable {

	public void run() {
		// TODO Auto-generated method stub
		for (int i = 1; i < 20; i++) {
			System.out.println("times of calling thread of Runner:" + i);
		}
	}
}

The result is below:

we can find than it's excute the method not construct a new thread

times of calling thread of Runner:1
times of calling thread of Runner:2
times of calling thread of Runner:3
times of calling thread of Runner:4
times of calling thread of Runner:5
times of calling thread of Runner:6
times of calling thread of Runner:7
times of calling thread of Runner:8
times of calling thread of Runner:9
times of calling thread of Runner:10
times of calling thread of Runner:11
times of calling thread of Runner:12
times of calling thread of Runner:13
times of calling thread of Runner:14
times of calling thread of Runner:15
times of calling thread of Runner:16
times of calling thread of Runner:17
times of calling thread of Runner:18
times of calling thread of Runner:19
times of calling main thread1
times of calling main thread2
times of calling main thread3
times of calling main thread4
times of calling main thread5
times of calling main thread6
times of calling main thread7
times of calling main thread8
times of calling main thread9
times of calling main thread10
times of calling main thread11
times of calling main thread12
times of calling main thread13
times of calling main thread14
times of calling main thread15
times of calling main thread16
times of calling main thread17
times of calling main thread18
times of calling main thread19

 

The method Two:

 

public class testThread2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Runner3 r1 = new Runner3();
		r1.start();
		for (int i = 1; i < 50; i++) {
			System.out.println("times of calling main thread" + i);
		}
	}
}

class Runner3 extends Thread {
	public void run() {
		// TODO Auto-generated method stub
		for (int i = 1; i < 50; i++) {
			System.out.println("times of calling thread of Runner:" + i);
		}
	}
}

 

the result is similar with the method one

论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics