进行和线程
多进程
操作系统中能同时运行多个任务(程序)
多线程:
同一应用程序中有多个顺序流(同时)执行。
创建线程的方法:
方式一:定义一个线程类,它继承类Thread并重写其中的方法run(),方法run()成为线程体。
由于java只支持但继承,用这种方法继承的类不能再继承其他类。
启动线程宜定盈要用start方法不能用其他方法。
public class FirstThread extends Thread{
public void run(){
for(int i =0;i < 100;i++){
System.out.println("FirstThread" + "----" + i);
}
}
}
class Test{
public static void main(String[] args){
//生成线程类的对象
FirstThread ft = new FirstThread();
//启动线程必须使用start方法
ft.start();
//主函数一个线程,对象ft是一个线程,垃圾回收线程。
for(int i =0;i < 100;i++){
System.out.println("main" + "---" + i);
}
}
}