作者简介: zoro-1,目前大二,正在学习Java,数据结构,mysql,javaee等
作者主页: zoro-1的主页
欢迎大家点赞 收藏 ⭐ 加关注哦!
class myThread extends Thread{
@Override
public void run() {
System.out.println("myThread");
}
}
public class TreadText {
public static void main(String[] args) {
//自己继承Thread重写run方法
Thread thread=new myThread();
thread.start();
}
}
class myRunnable implements Runnable{
@Override
public void run() {
System.out.println("myRunnable");
}
}
public class TreadText {
public static void main(String[] args) {
//实现Runnable相当于一个代码块还是用Thread创建线程
Thread thread2=new Thread(new myRunnable());
thread2.start();
}
}
public class TreadText {
public static void main(String[] args) {
//传一个继承Thread匿名内部类
Thread thread4=new Thread(new Thread(){
public void run() {
while (true) {
System.out.println("thread4");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
});
thread4.start();
}
}
public class TreadText {
public static void main(String[] args) {
//传一个实现Runnable匿名内部类
Thread thread3=new Thread(new Runnable(){
@Override
public void run() {
System.out.println("thread3");
}
});
thread3.start();
}
}
public class TreadText {
public static void main(String[] args) {
//函数接口底层还是用Ruanable
Thread thread1=new Thread(()-> System.out.println("thread1"));
thread1.start();
}
}
今天的分享到此就结束了,感谢大家支持