首页 > 代码库 > Thread和Runnable

Thread和Runnable

class TestThread extends Thread{
  public void run(){
    for(int i=0;i<10;i++){
      System.out.println(Thread.currentThread().getName()+"---is running");
    }
  }
}
public class Thread01 {
  public static void main(String[] args) {
    TestThread tt1 = new TestThread();
    TestThread tt2 = new TestThread();
    tt1.start();
    tt2.start();
    new Thread(new Runnable() {
          @Override
        public void run() {
          for(int i=0;i<20;i++){
            System.out.println(Thread.currentThread().getName()+"----is running");
          }
      }
    }).start();
  }
}

运行结果:

Thread-1---is running
Thread-2----is running
Thread-0---is running
Thread-2----is running
Thread-1---is running
Thread-2----is running
Thread-0---is running
Thread-2----is running
Thread-1---is running
Thread-2----is running
Thread-2----is running
Thread-0---is running
Thread-2----is running
Thread-1---is running
Thread-2----is running
Thread-0---is running
Thread-2----is running
Thread-1---is running
Thread-2----is running
Thread-0---is running
Thread-2----is running
Thread-1---is running
Thread-2----is running
Thread-0---is running
Thread-2----is running
Thread-1---is running
Thread-2----is running
Thread-0---is running
Thread-2----is running
Thread-1---is running
Thread-2----is running
Thread-0---is running
Thread-2----is running
Thread-1---is running
Thread-2----is running
Thread-0---is running
Thread-2----is running
Thread-1---is running
Thread-2----is running
Thread-0---is running

Thread和Runnable