package com.brendan.cn.concurrent;
public class TestThread {
public static void main(String[] args) {
//第一种方式
Thread thread = new Thread(){
@Override
public void run() {
while(true){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("1:" + Thread.currentThread().getName());
}
}
};
thread.start();
//第二种方式
//一般选第二种方式
Thread thread2 = new Thread(new Runnable(){
@Override
public void run() {
while(true){
try {
Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("1:" + Thread.currentThread().getName());
}
}
});
thread2.start();
}
}
java中创建线程的两种方式
最新推荐文章于 2023-06-09 20:38:03 发布