Java线程

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Java线程

淡沫初夏Zz   2022-06-03 我要评论

一、创建线程

启动线程—start 方法

通过覆写 run 方法创建⼀个线程对象,但线程对象被创建出来并不意味着线程就开始运行了

  • 覆写run方法是给线程指令清单
  • 但是start方法,则是让线程去真正的执行

方法一

继承Thread类

/**
 * 继承Thread创建线程
 */
class MyThread1 extends Thread{
    @Override
    public void run() {
        //业务代码
        Thread thread = Thread.currentThread();
        System.out.println("名称:" + thread.getName());
    }
}
public class ThreadDemo1 {
    public static void main(String[] args) {
        //获得当前的线程
        Thread mainThread = Thread.currentThread();
        System.out.println("名称:" + mainThread.getName());
        Thread thread = new MyThread1();
        //开启线程
        thread.start();
    }
}

因为 Java 是单继承,继承了 Thread 就不能继承其他类了,然而 Java 可以实现多个接口,于是有了下⼀种方式

方法二

实现Runnable接口

/**
 * 使用Runnable接口创建线程
 */
class MyThread2 implements Runnable{
    @Override
    public void run() {
        Thread thread = Thread.currentThread();//得到当前线程
        System.out.println("名称:" + thread.getName());
    }
}
public class ThreadDemo2 {
    public static void main(String[] args) {
        MyThread2 myThread2 = new MyThread2();
        //创建线程
        Thread thread = new Thread(myThread2);
        //启动线程
        thread.start();
    }
}

方法三

继承Thread类使用匿名内部类

/**
 * 继承Thread使用匿名内部类创建
 */
public class ThreadDemo4 {
    public static void main(String[] args) {
        Thread thread = new Thread(){
            @Override
            public void run() {
//                业务代码
                Thread thread2 = Thread.currentThread();
                System.out.println("名称" + thread2.getName());
            }
        };
        //开始线程
        thread.start();

    }
}

方法四

实现Runnable接口,使用匿名内部类

/**
 * runnable使用匿名内部类创建
 */
public class ThreadDemo3 {
    public static void main(String[] args) {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                //具体业务
                Thread thread1 = Thread.currentThread();
                System.out.println("名称:" + thread1.getName());
            }
        });
        //开启线程
        thread.start();
    }
}

方法五

使用lambda表达式

/**
 * 使用lambda表达式
 */
public class ThreadDemo5 {
    public static void main(String[] args) {
        //创建线程
        Thread thread = new Thread(()->{
            //业务代码
            Thread thread3 = Thread.currentThread();
            System.out.println("名称" + thread3.getName());
        });
        //启动线程
        thread.start();
    }
}

方法六

带返回值的 Callable

import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * 使用callable创建线程
 */
public class ThreadDemo6 {
    public static void main(String[] args)throws ExecutionException, InterruptedException {
        // 创建 Callable 实例
        MyCallable callable = new MyCallable();
        // 用于接收 Callable 结果的对象
        FutureTask<Integer> futureTask = new FutureTask<Integer>(callable);
        // 创建新线程
        Thread thread = new Thread(futureTask);
        // 启动线程
        thread.start();
        // 接收新线程执行的结果
        int result = futureTask.get();
        System.out.println(Thread.currentThread().getName() +
                "——新线程返回的结果为:" + result);
    }
}
/**
 * Callable<V> 泛型里面可以是任意数据类型
 */
class MyCallable implements Callable<Integer> {
    @Override
    public Integer call() throws Exception {
        // 生成随机数:0-9
        int randomNum = new Random().nextInt(10);
        System.out.println(Thread.currentThread().getName() +
                "——随机数:" + randomNum);
        return randomNum;
    }
}

在创建线程时, 如果是 JDK 1.8 以上版本,在不需要获得线程执行结果的情况下,推荐使用Lambda 方式来创建线程,因为它的写法足够简洁;如果想要获取线程执行结果,可使用FutureTask + Callable 的方式来实现

二、run方法和start方法的区别

run 方法和 start 方法的主要区别如下:

①方法性质不同

run 是一个普通方法,而 start 是开启新线程的方法。

②执行速度不同

调用 run 方法会立即执行任务,调用 start 方法是将线程的状态改为就绪状态,不会立即执行。

③调用次数不同

run 方法可以被重复调用,而 start 方法只能被调用一次。start 方法之所以不能被重复调用的原因是,线程的状态是不可逆的,Thread 在 start 的实现源码中做了判断,如果线程不是新建状态 NEW,则会抛出非法线程状态异常IllegalThreadStateException

  public static void main(String[] args) {
        // 创建线程一
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                // 获取到当前执行的线程
                Thread currThread = Thread.currentThread();
                System.out.println("执行线程一,线程名:" + currThread.getName());
            }
        });
        // 调用 run 方法
        thread.run();
        // 多次调用 run 方法
        thread.run();

        // 创建线程二
        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                // 获取到当前执行的线程
                Thread currThread = Thread.currentThread();
                System.out.println("执行线程二,线程名:" + currThread.getName());
            }
        });
       // 调用 start 方法
        thread2.start();
       // 多次调用 start 方法
        thread2.start();
    }

从上述结果可以看出,run 方法多次调用可用正常执行,而第二次调用 start 方法时程序就报错了,提示“IllegalThreadStateException”非法线程状态异常

总结

方法性质不同:run 是一个普通方法,而 start 是开启新线程的方法。

执行速度不同:调用 run 方法会立即执行任务,调用 start 方法是将线程的状态改为就绪状态,不会立即执行。

调用次数不同:run 方法可以被重复调用,而 start 方法只能被调用一次。

三、线程的构造方法

1、Thread()创建线程

Thread t1 = new Thread();

2、Thread(Runnable target) 创建线程

Thread t2 = new Thread(new MyRunnable());

3、Thread(String name)创建线程且命名

/**
 * 创建线程,构造方法设置线程名称
 */
public class ThreadDemo9 {
    public static void main(String[] args) {
        //构造方法设置名称
        Thread thread = new Thread("线程1"){
            @Override
            public void run() {
                //休眠线程
                try {
                    Thread.sleep(1000*60*60);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        thread.start();
    }
}

4、Thread(Runnable target, String name),用Runnable 对象创建线程对象,并命名

/**
 * 创建线程,并设置名称
 */
public class ThreadDemo10 {
    public static void main(String[] args) {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000*60*60);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"Runable-Thread");
        thread.start();
    }
}

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们