办法一:
package com.xiao.testThread; //创建线程方式一:继承Thread类,重写run()办法,调用start开启线程 //总结:注意,线程开启不一定立即执行,由CPU调度执行 public class TestThread1 extends Thread{ public void run(){ //run方法线程体 for (int i = 0; i <200 ; i++) { System.out.println("我在看代码----"+i); } } public static void main(String[] args) { //main线程,主线程 //创建一个线程对象 TestThread1 testThread1=new TestThread1(); //调用start()方法开启线程 testThread1.start(); for (int i = 0; i <1000 ; i++) { System.out.println("我是小小"+i); } } }
办法二:
package com.xiao.testThread; //创建线程方式2:实现runnable接口,重写run方法, public class TestThread2 implements Runnable{ public void run(){ //run方法线程体 for (int i = 0; i <200 ; i++) { System.out.println("我在看代码----"+i); } } public static void main(String[] args) { //main线程,主线程 //创建一个线程对象 TestThread2 testThread2=new TestThread2(); //调用start()方法开启线程 new Thread(testThread2).start(); for (int i = 0; i <1000 ; i++) { System.out.println("我是小小"+i); } } }

微信扫码关注
更新实时通知