方法 |
用途 |
static Thread currentThread() |
得到当前线程 |
getName() |
返回线程的名称 |
setName(String name) |
将线程的名称设置为由name指定的名称 |
start( ) |
调用run( )方法启动线程,开始线程的执行 |
run( ) |
存放线程体代码 |
Thread.sleep(1000) |
1秒 |
TimeUnit.SECONDS.sleep(1); |
1秒 |
public static void main(String[] args) {
new Thread(()->{
try {
TimeUnit.SECONDS.sleep(1);
System.out.println("thread2");
//输出线程名
System.out.println(Thread.currentThread().getName());
//线程级别1-10
System.out.println(Thread.currentThread().getPriority());
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
//线程级别
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
//线程名称
Thread.currentThread().setName("超级线程");
try {
//Thread.sleep(1000);
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
// new Thread(()->System.out.println("thread")).start();
System.out.println("hello word");
//输出线程名
System.out.println(Thread.currentThread().getName());
//线程级别
System.out.println(Thread.currentThread().getPriority());
}