一,将任务放在Thread子类中
package Thread;
import java.lang.Thread;
public class thread extends Thread { //继承Thread类
public void run(){ //编写run函数体
while(true){
System.out.println("1");
}
}
public static void main(String args[]){ //main函数
thread t1=new thread(); //创建一个线程实例
t1.start(); //启动先线程
while(true){ //下面运行main函数的代码
System.out.println("2");
}
}
}
============================================================
二,讲任务放在实现了runnable接口类中
package Interface;
public class interFace implements Runnable { //实现Runnable接口
public void run(){ //编写run函数体
while(true){
System.out.println("1");
}
}
public static void main(String args[]){ //main函数
interFace t1=new interFace(); //实例化接口类
Thread t=new Thread(t1); //又接口实例创建一个线程体
t.start(); //启动线程
while(true){ //下面是main的代码
System.out.println("2");
}
}
}