スレッドエッセイ-スレッドを作成する基本的な方法

6358 ワード

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:1times of calling main thread1times of calling thread of Runner:2times of calling main thread2times of calling thread of Runner:3times of calling main thread3times of calling thread of Runner:4times of calling main thread4times of calling thread of Runner:5times of calling main thread5times of calling thread of Runner:6times of calling main thread6times of calling thread of Runner:7times of calling main thread7times of calling thread of Runner:8times of calling main thread8times of calling thread of Runner:9times of calling main thread9times of calling thread of Runner:10times of calling main thread10times of calling thread of Runner:11times of calling main thread11times of calling thread of Runner:12times of calling main thread12times of calling thread of Runner:13times of calling main thread13times of calling thread of Runner:14times of calling main thread14times of calling thread of Runner:15times of calling main thread15times of calling thread of Runner:16times of calling main thread16times of calling thread of Runner:17times of calling main thread17times of calling thread of Runner:18times of calling main thread18times of calling thread of Runner:19times of calling main thread19times of calling thread of Runner:20times of calling thread of Runner:21times of calling thread of Runner:22times of calling thread of Runner:23times of calling thread of Runner:24times of calling thread of Runner:25times of calling thread of Runner:26times of calling thread of Runner:27times of calling thread of Runner:28times of calling main thread20times of calling thread of Runner:29times of calling main thread21times of calling thread of Runner:30times of calling main thread22times of calling thread of Runner:31times of calling main thread23times of calling main thread24times of calling main thread25times of calling main thread26times of calling main thread27times of calling main thread28times of calling main thread29times of calling main thread30times of calling thread of Runner:32times of calling main thread31times of calling thread of Runner:33times of calling main thread32times of calling thread of Runner:34times of calling thread of Runner:35times of calling thread of Runner:36times of calling thread of Runner:37times of calling thread of Runner:38times of calling thread of Runner:39times of calling main thread33times of calling thread of Runner:40times of calling main thread34times of calling thread of Runner:41times of calling main thread35times of calling thread of Runner:42times of calling main thread36times of calling thread of Runner:43times of calling main thread37times of calling thread of Runner:44times of calling main thread38times of calling thread of Runner:45times of calling main thread39times of calling thread of Runner:46times of calling main thread40times of calling thread of Runner:47times of calling main thread41times of calling thread of Runner:48times of calling main thread42times of calling thread of Runner:49times of calling main thread43times of calling main thread44times of calling main thread45times of calling main thread46times of calling main thread47times of calling main thread48times 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:1times of calling thread of Runner:2times of calling thread of Runner:3times of calling thread of Runner:4times of calling thread of Runner:5times of calling thread of Runner:6times of calling thread of Runner:7times of calling thread of Runner:8times of calling thread of Runner:9times of calling thread of Runner:10times of calling thread of Runner:11times of calling thread of Runner:12times of calling thread of Runner:13times of calling thread of Runner:14times of calling thread of Runner:15times of calling thread of Runner:16times of calling thread of Runner:17times of calling thread of Runner:18times of calling thread of Runner:19times of calling main thread1times of calling main thread2times of calling main thread3times of calling main thread4times of calling main thread5times of calling main thread6times of calling main thread7times of calling main thread8times of calling main thread9times of calling main thread10times of calling main thread11times of calling main thread12times of calling main thread13times of calling main thread14times of calling main thread15times of calling main thread16times of calling main thread17times of calling main thread18times 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