Thread线程创建三种方式 java 继承Thread类 实现Runnable接口 实现Callable接口

Thread 线程创建三种方式 java

继承Thread类

线程对象将会开始争抢资源,这个线程要执行的任务要放在方法中并且这个方法不能是随便写的一个方法,必须是重写Thread类中的run方法,线程的任务/逻辑写在run方法中

子线程创建
public class CT01 extends Thread {
    
    
    @Override
    public void run() {
    
    
        //子线程输出
        System.out.println("Thread-----this is thread");
    }
}
主线程调用

如果调用run方法去执行线程中的任务是不行的,直接调用run()就会被当做一个普通方法,不会出现线程抢资源

public class Test {
    
    
    public static void main(String[] args) {
    
    
        //主线程第一段输出:
        System.out.println("main1-----");

        //制造其他线程,要跟主线程争抢资源,具体的线程对象:子线程
        CT01 ct = new CT01();
        ct.run();
        //主线程第二段输出:
        System.out.println("main2-----");
    }
}
main1-----
Thread-----this is thread
main2-----

想要ct子线程真正起作用比如要启动线程,就要使用start()方法,start()是Thread类中的方法

public class Test {
    
    
    public static void main(String[] args) {
    
    
        //主线程第一段输出:
        System.out.println("main1-----");

        //制造其他线程,要跟主线程争抢资源,具体的线程对象:子线程
        CT01 ct = new CT01();
        //
        ct.start();
        //主线程第二段输出:
        System.out.println("main2-----");
    }
}
main1-----
main2-----
Thread-----this is thread

java程序的线程状况如下,即使我们只是用一个线程,实际上也会有异常处理线程和垃圾清理线程,并且异常线程会影响主线程的执行

在这里插入图片描述

实现Runnable接口

子线程调用
public class CT02 implements Runnable{
    
    
    @Override
    public void run() {
    
    
        System.out.println("Thread-----This is Runnable");
    }
}
主线程调用

在这里不能直接使用start()调用,因为创建出来的CT02类中并没有start()方法,该方法在Thread类中。需要转成Thread,而thread类提供了public Thread(Runnable target)的构造器刚好能以Runnable实现类为基础创建。start()经过层层嵌套会最终转到实现类中的run()方法

public class Test {
    
    
    public static void main(String[] args) {
    
    
        //主线程第一段输出:
        System.out.println("main1-----");
        CT02 ct02 = new CT02();
        Thread ct = new Thread(ct02);//Thread(Runnable target)构造器
        ct.start();
        //主线程第二段输出:
        System.out.println("main2-----");
    }
}
main1-----
main2-----
Thread-----This is Runnable

实现Callable接口

对比第一种和第二种创建线程的方式发现,无论继承Thread类的方式还是实现Runnable接口的方式,都需要有一个run()方法:

@Override
public void run() {
    
     }

但这个方法一没有返回值,二不能抛出异常。因此JDK1.5以后出现了实现Callable接口的第三种线程创建方式。

  • 实现Callable接口,有泛型选项
    • 如果带泛型,那么call()的返回值就是泛型对应的类型
    • 如果不带泛型,那么call()方式的返回值就默认是Object类型
  • call()方法有返回值,可以抛出异常,但线程创建比较麻烦
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class CallTest implements Callable<String> {
    
    
    @Override
    public String call() throws Exception {
    
    
        return "Thread-----This is Callable";
    }
}
class Test{
    
    
    //这是main方法,程序的入口
    public static void main(String[] args) throws ExecutionException, InterruptedException {
    
    
        //定义一个线程对象,调用时需要经过三层嵌套
        CallTest ct = new CallTest();
        FutureTask ft = new FutureTask(ct);
        Thread t = new Thread(ft);
        t.start();

        //获取线程得到的返回值:
        Object obj = ft.get();
        System.out.println(obj);
    }
}

猜你喜欢

转载自blog.csdn.net/m0_46530662/article/details/121166777