本文共 853 字,大约阅读时间需要 2 分钟。
方法1
package cn.sxt.TestT;/** * 声明一个Thread的子类 * 重写run方法 * 对象调用start方法 * @author 佳哥 * */public class SonThread extends Thread{ //重写run方法 public void run() { for(int i = 0; i<10; i ++) { System.out.println("跑啊 +" + i); } } public static void main(String[] args) { SonThread st = new SonThread(); st.start(); for(int i = 0; i<10; i ++) { System.out.println("主方法啊"+i); }}}
方法2
package cn.sxt.TestT;/** * 声明一个类实现Runnable接口 * 重写run方法 * 但需要和方法1一样需要掉用调用start方法来启动线程,所以需要创建一个thread对象 * @author 佳哥 * */public class ImplementsThread implements Runnable{ //重写run方法 public void run() { for(int i = 0; i<10; i ++) { System.out.println("跑啊 +" + i); } } public static void main(String[] args) { ImplementsThread it = new ImplementsThread(); new Thread(it).start(); for(int i = 0; i<10; i ++) { System.out.println("主方法啊"+i); }}}
转载地址:http://lgeq.baihongyu.com/