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());
System.out.println("2:"+this.getName());
}
}
};
thread.start();
第二种方法
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
while(true){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("1:"+Thread.currentThread().getName());
}
}
});
thread2.start();
创建子类并在里面写方法
new Thread(
/*这里的方法不运行*/
new Runnable() {
@Override
public void run() {
while(true){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("**********:"+Thread.currentThread().getName());
}
}
}){
/*运行这里的方法*/
public void run() {
while(true){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("8888888888:"+Thread.currentThread().getName());
}
}
}.start();