多线程 Runnable 实现线程 内部类

时间:2024-12-23 19:06:20
public class ThreadDemo4 {
public static void main(String[] args){
System.out.println(Thread.currentThread().getName());//main
Thread.currentThread().setName("haha");
String name = Thread.currentThread().getName(); // haha
Thread t = Thread.currentThread();
System.out.println(t.getPriority());// 5 线程默认优先级是 5
t.setPriority(10); // 设置优先级 1 - 10
System.out.println(t.getPriority());
System.out.println(name); MyRunable m1 = new MyRunable();
Thread t1 = new Thread(m1);
t1.start();
System.out.println(t1.getName()); //Thread-0 // 匿名内部类对象实现
new Thread(
new Runnable(){
public void run(){
System.out.println(Thread.currentThread().getName());//Thread-1
System.out.println("我是匿名内部类");
}
}
).start(); //匿名类
new Thread(){
public void run(){
System.out.println(Thread.currentThread().getName());//Thread-2
System.out.println("匿名类");
}
}.start(); }
} //Runable 接口 实现线程 class MyRunable implements Runnable{
public void run(){
System.out.println("helloworld");
}
}