TestThread.java 813 B

1234567891011121314151617181920212223242526272829
  1. package com.sf.thread_new;
  2. public class TestThread extends Thread{
  3. public TestThread(){}
  4. public TestThread(String name){
  5. // 名字是父类提供的属性
  6. // Thread(name) 是父类提供的构造器
  7. super(name);
  8. }
  9. @Override
  10. public void run() {
  11. // Thread-0 Thread-1
  12. System.out.println(getName() + " is running");
  13. }
  14. public static void main(String[] args) {
  15. // main
  16. System.out.println(Thread.currentThread().getName() + " is running");
  17. // 创建我们定义的类的线程
  18. TestThread thread1 = new TestThread();
  19. // 直接调用run方法相当于调用函数 不能触发多线程的执行
  20. thread1.start();
  21. TestThread thread2 = new TestThread("thread2");
  22. thread2.start();
  23. }
  24. }