一、继承Thread类创建线程类
①定义Thread类的子类,并重写run方法。
②创建Thread子类的实例。
③调用start()方法。
访问当前线程:Thread.currentThread().
第二种方法:实现Runnable接口,重写run()方法,run()方法代表线程要执行的任务。
可以这样写:启动 new Thread(Runnable r,String name).start();
public class ThreadInfo {
2
3 public static void main(String[] args) {
4
5 Thread t1 = new Thread(new Runnable() {
6
7 @Override
8 public void run() {
9 synchronized (ThreadInfo.class) {
10 System.out.println("Thread");
11 }
12 }
13 });
14
15 t1.start();
16
17
18 System.out.println(t1.getId());
19 System.out.println(t1.getName());
20 System.out.println(t1.getPriority());//优先级 1-10
21 System.out.println(t1.getState());//线程状态: NEW TERMINATED RUNNABLE TIMED_WAITING WAITING BLOCKED
22 }
23 }