Java多线程-run方法与start方法的区别

  1. package com.interview;  
  2. /** 
  3.  * java多线程的两种实现方式以及run、start方法的区别 
  4.  * @author MEI.LIU 
  5.  * 
  6.  */  
  7. public class ThreadTest {  
  8.     public static void main(String[] args) throws InterruptedException {  
  9.         ThreadTest tt = new ThreadTest();  
  10.         Thread1 t1 = tt.new Thread1();  
  11.         Thread2 t2 = tt.new Thread2();  
  12.         Thread3 t3 = tt.new Thread3();  
  13.         Thread4 t4 = tt.new Thread4();  
  14.         //实现Runnable方法的方式必须要用new Thread(tt.new Thread5())来转换为Thread类型,才能调用Thread的start方法)  
  15.         Thread t5 = new Thread(tt.new Thread5());  
  16.         t1.start();  
  17.         Thread.sleep(1000);  
  18.         t2.start();  
  19.         Thread.sleep(1000);  
  20.         t3.start();  
  21.         Thread.sleep(1000);  
  22.         t4.start();  
  23.         Thread.sleep(10000);  
  24.         ((Thread) t5).start();  
  25.     }  
  26.   
  27.     class Thread1 extends Thread {  
  28.         public void run() {  
  29.             try {  
  30.                 while (true) {  
  31.                     for (int i = 1000;; i--) {  
  32.                         Thread.sleep(1000);  
  33.                         System.out.println("现在i的值为:" + i);  
  34.                     }  
  35.                 }  
  36.             } catch (InterruptedException e) {  
  37.                 e.printStackTrace();  
  38.             }  
  39.         }  
  40.     }  
  41.   
  42.     class Thread2 extends Thread {  
  43.         public void run() {  
  44.             try {  
  45.                 while (true) {  
  46.                     System.out.println("哈哈哈哈哈哈哈哈哈");  
  47.                     Thread.sleep(666);  
  48.                 }  
  49.             } catch (InterruptedException e) {  
  50.                 e.printStackTrace();  
  51.             }  
  52.         }  
  53.     }  
  54.   
  55.     class Thread3 extends Thread {  
  56.         public void run() {  
  57.             try {  
  58.                 while (true) {  
  59.                     for (int j = 1000;; j--) {  
  60.                         System.out.println("目前,j的值是:" + j);  
  61.                         Thread.sleep(2000);  
  62.                     }  
  63.                 }  
  64.             } catch (InterruptedException e) {  
  65.                 e.printStackTrace();  
  66.             }  
  67.         }  
  68.     }  
  69.   
  70.     class Thread4 extends Thread {  
  71.         public void run() {  
  72.             try {  
  73.                 while (true) {  
  74.                     System.out.println("TTTTTTTTTTTTTTTTTTT");  
  75.                     Thread.sleep(5000);  
  76.                 }  
  77.             } catch (InterruptedException e) {  
  78.                 e.printStackTrace();  
  79.             }  
  80.         }  
  81.     }  
  82.   
  83.     class Thread5 implements Runnable {  
  84.         public void run() {  
  85.             try {  
  86.                 while (true) {  
  87.                     System.out.println("咚咚咚咚咚咚咚咚咚咚咚咚咚");  
  88.                     Thread.sleep(1400);  
  89.                 }  
  90.             } catch (InterruptedException e) {  
  91.                 e.printStackTrace();  
  92.             }  
  93.         }  
  94.     }  
  95. }  
package com.interview;
/**
 * java多线程的两种实现方式以及run、start方法的区别
 * @author MEI.LIU
 *
 */
public class ThreadTest {
	public static void main(String[] args) throws InterruptedException {
		ThreadTest tt = new ThreadTest();
		Thread1 t1 = tt.new Thread1();
		Thread2 t2 = tt.new Thread2();
		Thread3 t3 = tt.new Thread3();
		Thread4 t4 = tt.new Thread4();
		//实现Runnable方法的方式必须要用new Thread(tt.new Thread5())来转换为Thread类型,才能调用Thread的start方法)
		Thread t5 = new Thread(tt.new Thread5());
		t1.start();
		Thread.sleep(1000);
		t2.start();
		Thread.sleep(1000);
		t3.start();
		Thread.sleep(1000);
		t4.start();
		Thread.sleep(10000);
		((Thread) t5).start();
	}

	class Thread1 extends Thread {
		public void run() {
			try {
				while (true) {
					for (int i = 1000;; i--) {
						Thread.sleep(1000);
						System.out.println("现在i的值为:" + i);
					}
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	class Thread2 extends Thread {
		public void run() {
			try {
				while (true) {
					System.out.println("哈哈哈哈哈哈哈哈哈");
					Thread.sleep(666);
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	class Thread3 extends Thread {
		public void run() {
			try {
				while (true) {
					for (int j = 1000;; j--) {
						System.out.println("目前,j的值是:" + j);
						Thread.sleep(2000);
					}
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	class Thread4 extends Thread {
		public void run() {
			try {
				while (true) {
					System.out.println("TTTTTTTTTTTTTTTTTTT");
					Thread.sleep(5000);
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	class Thread5 implements Runnable {
		public void run() {
			try {
				while (true) {
					System.out.println("咚咚咚咚咚咚咚咚咚咚咚咚咚");
					Thread.sleep(1400);
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}

区别:调用start方法实现多线程,而调用run方法没有实现多线程

    Start:

    用start方法来启动线程,真正实现了多线程运行,这时无需等待run方法体代码执行完毕而直接继续执行下面的代码。通过调用Thread类的start()方法来启动一个线程,这时此线程处于就绪(可运行)状态,并没有运行,一旦得到spu时间片,就开始执行run()方法,这里方法run()称为线程体,它包含了要执行的这个线程的内容,Run方法运行结束,此线程随即终止。

    Run:

    run()方法只是类的一个普通方法而已,如果直接调用Run方法,程序中依然只有主线程这一个线程,其程序执行路径还是只有一条,还是要顺序执行,还是要等待run方法体执行完毕后才可继续执行下面的代码,这样就没有达到写线程的目的。

    总结:调用start方法方可启动线程,而run方法只是thread的一个普通方法调用,还是在主线程里执行。

 控制台输出结果:

 

posted on 2013-11-10 19:58  heartstage  阅读(502)  评论(0编辑  收藏  举报

导航