创建线程的几种方式,与常用函数介绍

创建线程两种方式

  1. 实现Runnable
  2. 继承Thread

可能还有朋友说还有通过Callable和Future创建线程和线程池创建线程,实际上这两个方式的实现都是通过上面两中方式实现的

实现Runnable

优点

线程只是实现Runnable或实现Callable接口,还可以继承其他类,扩展性好。

缺点:

相比与继承继承Thread实现稍微复杂

public class FirstThreadTest implements Runnable{


    @Override
    public void run() {
        System.out.println("我是FirstThreadTest线程");
    }

    public static void main(String[] args) {
        Thread thread=new Thread(new FirstThreadTest());
        thread.start();
    }
}

继承Thread

优点

编写简单,如果需要访问当前线程,则无需使用Thread.currentThread()方法,直接使用this即可获得当前线程。

缺点:

线程类已经继承了Thread类,所以不能再继承其他父类。

public class FirstThreadTest extends Thread{


    @Override
    public void run() {
        System.out.println("我是FirstThreadTest线程");
    }

    public static void main(String[] args) {
        FirstThreadTest firstThreadTest=new FirstThreadTest();
        firstThreadTest.start();
    }
}

通过Callable和Future创建线程

public class FirstThreadTest implements Callable<Integer> {


    @Override
    public Integer call() throws Exception {
        System.out.println("这是FirstThreadTest线程");
        return null;
    }

    public static void main(String[] args) {
        FirstThreadTest ctt = new FirstThreadTest();
        FutureTask<Integer> ft = new FutureTask<>(ctt);
        new Thread(ft).start();
    }
    
}

常用函数

  1. Thread.currentThread()

Thread.currentThread()

作用:返回对当前正在执行的线程对象的引用。

可以用来查看当前函数正在被那个线程正在调用信息
例如:

public class FirstThreadTest implements Callable<Integer> {


    @Override
    public Integer call() throws Exception {
        System.out.println("这是FirstThreadTest线程"+ Thread.currentThread().getName());

        return null;
    }

    public static void main(String[] args) {
        FirstThreadTest ctt = new FirstThreadTest();
        FutureTask<Integer> ft = new FutureTask<>(ctt);
        Thread thread=     new Thread(ft);
        thread.setName("FirstThread");
        thread.start();
    }

}

运行结果:这是FirstThreadTest线程FirstThread
当然还可以获取线程id等

public final boolean isAlive()

测试这个线程是否活着。 如果一个线程已经启动并且尚未死亡,线程处于运行,或准备运行状态,那么线程是活着的。

例如

public class FirstThreadTest extends Thread {


    @Override
    public void run()  {
        //线程处于运行阶段
        System.out.println("这是FirstThreadTest线程isAlive1 :"+ Thread.currentThread().isAlive());
    }

    public static void main(String[] args) {
        FirstThreadTest thread=new FirstThreadTest();
        thread.start();//启动线程 线程处于准备运行阶段
        System.out.println("这是FirstThreadTest线程isAlive2 :"+ thread.isAlive());
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //线程已终止
        System.out.println("这是FirstThreadTest线程isAlive3 :"+ thread.isAlive());
    }

}

public static void sleep(long millis)

使当前正在执行的线程以指定的毫秒数暂停(暂时停止执行),具体取决于系统定时器和调度程序的精度和准确性。 线程不会丢失任何显示器的所有权。

public final void stop() 这个方法已过时

停止当前线程,该方法最初设计为强制线程停止并抛出一个给定的Throwable作为例外。 它本质上是不安全的;
如何优雅的停止线程后面在说

posted @ 2020-08-08 14:40  jelly_wt  阅读(398)  评论(0)    收藏  举报