java中线程的创建
作者:互联网
创建方式一:创建一个类去继承 Thread类
public class MyThread1 extends Thread {
@Override
public void run() {
for (int i = 0; i < 20; i++) {
if (i % 2 == 0) {
System.out.println(currentThread().getName() + ":" + i);
}
}
}
public static void main(String[] args) {
MyThread1 myThread1 = new MyThread1();
myThread1.start();
}
}
创建方式二: 创建一个类去实现Runnable接口
public class MyThread1 implements Runnable {
@Override
public void run() {
for (int i = 0; i < 20; i++) {
if (i % 2 == 0) {
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}
}
public static void main(String[] args) {
MyThread1 myThread1 = new MyThread1();
Thread t1 = new Thread(myThread1)
t1.start();
}
}
标签:java,myThread1,Thread,创建,void,MyThread1,new,中线,public 来源: https://blog.csdn.net/weixin_53763209/article/details/117597143