Java语言进阶 互动版

在线工具推荐: Three.js AI纹理开发包 - YOLO合成数据生成器 - GLTF/GLB在线编辑 - 3D模型格式在线转换 - 可编程3D场景编辑器

Runable创建线程

创建一个线程,最简单的方法是创建一个实现Runnable接口的类。

实例:


public class testThread
{
  public static void main(String [] args){
      Runner1 r1 = new Runner1();
      Thread t = new Thread(r1);
      t.start();
      for(int i = 0; i< 10; i++){
          System.out.println("main thread = "+ i);
      }
  }
}

class Runner1  implements Runnable
{
    public void run(){
    for(int i = 0; i < 10; i++){
    System.out.println("Runner1 = " +i);
    }
    }
}

启动一个线程必须调用Thread类的start方法。

打印出上面例子的结果,引入java.lang.Runnable;