- 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();
- }
- }
- }
- }
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的一个普通方法调用,还是在主线程里执行。
控制台输出结果: