实现多线程的方法有2种:方法一是继承Thread,方法二是实现Runnerable接口
使用继承Thread类的方法创建多线程的时候,最大的局限性就是不支持多继承,因为java语言的特点就是单继承,因此为了支持多继承,完全可以实现Runnerabe接口的方式。
package com.cky.thread; /** * Created by chenkaiyang on 2017/12/2. */ public class MyFirstThread extends Thread{ @Override public void run() { super.run(); System.out.println("myThread"); } }
1 package com.cky.test; 2 3 import com.cky.thread.MyFirstThread; 4 5 /** 6 * Created by chenkaiyang on 2017/12/2. 7 */ 8 public class Test { 9 public static void main(String[] args) throws InterruptedException { 10 MyFirstThread th = new MyFirstThread(); 11 th.start(); 12 System.out.println("运行结束"); 13 } 14 }
D:itjdk1.8injava -Didea.launcher.port=7535 "-Didea.launcher.bin.path=D:itideaIntelliJ IDEA 2016.3.3in" -Dfile.encoding=UTF-8 -classpath "D:itjdk1.8jrelibcharsets.jar;D:itjdk1.8jrelibdeploy.jar;D:itjdk1.8jrelibextaccess-bridge-64.jar;D:itjdk1.8jrelibextcldrdata.jar;D:itjdk1.8jrelibextdnsns.jar;D:itjdk1.8jrelibextjaccess.jar;D:itjdk1.8jrelibextjfxrt.jar;D:itjdk1.8jrelibextlocaledata.jar;D:itjdk1.8jrelibext
ashorn.jar;D:itjdk1.8jrelibextsunec.jar;D:itjdk1.8jrelibextsunjce_provider.jar;D:itjdk1.8jrelibextsunmscapi.jar;D:itjdk1.8jrelibextsunpkcs11.jar;D:itjdk1.8jrelibextzipfs.jar;D:itjdk1.8jrelibjavaws.jar;D:itjdk1.8jrelibjce.jar;D:itjdk1.8jrelibjfr.jar;D:itjdk1.8jrelibjfxswt.jar;D:itjdk1.8jrelibjsse.jar;D:itjdk1.8jrelibmanagement-agent.jar;D:itjdk1.8jrelibplugin.jar;D:itjdk1.8jrelib
esources.jar;D:itjdk1.8jrelib
t.jar;F:springboot hreaddemooutproduction hreaddemo;D:itideaIntelliJ IDEA 2016.3.3libidea_rt.jar" com.intellij.rt.execution.application.AppMain com.cky.test.Test
运行结束
myThread
Process finished with exit code 0
从图中结果可知
MyFirstThread类中的run方法执行的比较晚,说明当开启线程的时候,cpu首先执行的是main线程,执行结束后,cpu切换到了另一个线程,这时再执行run方法中的代码
下面来通过代码来随机展示线程的随机性
先创建线程类
1 package com.cky.thread; 2 3 /** 4 * Created by chenkaiyang on 2017/11/27. 5 */ 6 public class Mythread extends Thread{ 7 @Override 8 public void run(){ 9 try { 10 for (int i=0; i<10;i++) { 11 int time = (int) (Math.random() * 1000); 12 Thread.sleep(time); 13 System.out.println("run="+ Thread.currentThread().getName()); 14 15 } 16 } catch (InterruptedException e) { 17 e.printStackTrace(); 18 } 19 } 20 }
再创建测试类
1 package com.cky.test; 2 3 import com.cky.thread.Mythread; 4 5 /** 6 * Created by chenkaiyang on 2017/11/27. 7 */ 8 public class Test1 { 9 public static void main(String[] args) { 10 try { 11 Thread thread = new Mythread(); 12 thread.setName("cky"); 13 thread.start(); 14 for (int i = 0; i < 10; i++) { 15 int time = (int) (Math.random() * 1000); 16 Thread.sleep(time); 17 System.out.println("main="+ Thread.currentThread().getName()); 18 19 } 20 } catch (InterruptedException e) { 21 e.printStackTrace(); 22 } 23 24 25 } 26 27 }
D:itjdk1.8injava -Didea.launcher.port=7533 "-Didea.launcher.bin.path=D:itideaIntelliJ IDEA 2016.3.3in" -Dfile.encoding=UTF-8 -classpath "D:itjdk1.8jrelibcharsets.jar;D:itjdk1.8jrelibdeploy.jar;D:itjdk1.8jrelibextaccess-bridge-64.jar;D:itjdk1.8jrelibextcldrdata.jar;D:itjdk1.8jrelibextdnsns.jar;D:itjdk1.8jrelibextjaccess.jar;D:itjdk1.8jrelibextjfxrt.jar;D:itjdk1.8jrelibextlocaledata.jar;D:itjdk1.8jrelibext
ashorn.jar;D:itjdk1.8jrelibextsunec.jar;D:itjdk1.8jrelibextsunjce_provider.jar;D:itjdk1.8jrelibextsunmscapi.jar;D:itjdk1.8jrelibextsunpkcs11.jar;D:itjdk1.8jrelibextzipfs.jar;D:itjdk1.8jrelibjavaws.jar;D:itjdk1.8jrelibjce.jar;D:itjdk1.8jrelibjfr.jar;D:itjdk1.8jrelibjfxswt.jar;D:itjdk1.8jrelibjsse.jar;D:itjdk1.8jrelibmanagement-agent.jar;D:itjdk1.8jrelibplugin.jar;D:itjdk1.8jrelib
esources.jar;D:itjdk1.8jrelib
t.jar;F:springboot hreaddemooutproduction hreaddemo;D:itideaIntelliJ IDEA 2016.3.3libidea_rt.jar" com.intellij.rt.execution.application.AppMain com.cky.test.Test1
run=cky
main=main
run=cky
run=cky
run=cky
main=main
run=cky
run=cky
main=main
run=cky
run=cky
run=cky
main=main
main=main
main=main
main=main
run=cky
main=main
main=main
main=main
Process finished with exit code 0
运行结果如上图:结果分析:代码用了随机数的方式用了Thread.sleep方法。该方法的目的是为了让当前执行的线程进行等待,这样,cpu就会切换到另一个线程。
注:执行start()方法的顺序也不能代表线程的启动顺序。
package com.cky.thread; /** * Created by chenkaiyang on 2017/12/2. */ public class MySecondThread extends Thread{ private int i; public MySecondThread (int i) { super(); this.i = i; } @Override public void run() { super.run(); System.out.println(i); } }
1 package com.cky.test; 2 3 import com.cky.thread.MySecondThread; 4 5 /** 6 * Created by chenkaiyang on 2017/12/2. 7 */ 8 public class Test { 9 public static void main(String[] args) throws InterruptedException { 10 MySecondThread t1 = new MySecondThread(1); 11 MySecondThread t2 = new MySecondThread(2); 12 MySecondThread t3 = new MySecondThread(3); 13 MySecondThread t4 = new MySecondThread(4); 14 MySecondThread t5 = new MySecondThread(5); 15 MySecondThread t6 = new MySecondThread(6); 16 MySecondThread t7 = new MySecondThread(7); 17 t1.start(); 18 t2.start(); 19 t3.start(); 20 t4.start(); 21 t5.start(); 22 t6.start(); 23 t7.start(); 24 } 25 }
D:itjdk1.8injava -Didea.launcher.port=7538 "-Didea.launcher.bin.path=D:itideaIntelliJ IDEA 2016.3.3in" -Dfile.encoding=UTF-8 -classpath "D:itjdk1.8jrelibcharsets.jar;D:itjdk1.8jrelibdeploy.jar;D:itjdk1.8jrelibextaccess-bridge-64.jar;D:itjdk1.8jrelibextcldrdata.jar;D:itjdk1.8jrelibextdnsns.jar;D:itjdk1.8jrelibextjaccess.jar;D:itjdk1.8jrelibextjfxrt.jar;D:itjdk1.8jrelibextlocaledata.jar;D:itjdk1.8jrelibext
ashorn.jar;D:itjdk1.8jrelibextsunec.jar;D:itjdk1.8jrelibextsunjce_provider.jar;D:itjdk1.8jrelibextsunmscapi.jar;D:itjdk1.8jrelibextsunpkcs11.jar;D:itjdk1.8jrelibextzipfs.jar;D:itjdk1.8jrelibjavaws.jar;D:itjdk1.8jrelibjce.jar;D:itjdk1.8jrelibjfr.jar;D:itjdk1.8jrelibjfxswt.jar;D:itjdk1.8jrelibjsse.jar;D:itjdk1.8jrelibmanagement-agent.jar;D:itjdk1.8jrelibplugin.jar;D:itjdk1.8jrelib
esources.jar;D:itjdk1.8jrelib
t.jar;F:springboot hreaddemooutproduction hreaddemo;D:itideaIntelliJ IDEA 2016.3.3libidea_rt.jar" com.intellij.rt.execution.application.AppMain com.cky.test.Test
1
3
4
5
2
7
6
Process finished with exit code 0
结果分析:执行start方法的顺序不能代表线程的启动顺序。