1)继承Thread类创建线程
如图所示:
//继承Thread类
class Aclass extends Thread{
//输出100以内的偶数
@Override
public void run() {
for (int i = 1; i <=100; i++) {
System.out.println(getName()+":"+i);
}
}
}
测试:
Thread t1 = new Aclass();
t1.setName("线程A");
t1.start();
输出:
2)实现Runnable接口,然后把Runnable接口的实现类作为参数,丢给Thread的构造器,创建Thread对象
如图:
//实现Runnable接口
class Bclass implements Runnable{
//输出100以内的偶数
@Override
public void run() {
for (int i = 1; i <=100; i++) {
if (i%2!=0){
System.out.println(Thread.current