栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

多线程(二)创建线程及线程的构造方法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

多线程(二)创建线程及线程的构造方法

目录

 一,创建线程

启动线程—start 方法方法一:继承Thread类方法二:实现Runnable接口方法三:继承Thread类,使用匿名内部类方法四:实现Runnable接口,使用匿名内部类方法五:使用lambda表达式方法六:带返回值的 Callable 二,run方法和start方法的区别

① 方法性质不同:②执行速度不同:③调用次数不同:总结  三,线程的构造方法

1,Thread()创建线程2,Thread(Runnable target) 创建线程3,Thread(String name)创建线程且命名4,Thread(Runnable target, String name),用Runnable 对象创建线程对象,并命名

 一,创建线程 启动线程—start 方法

通过覆写 run 方法创建⼀个线程对象,但线程对象被创建出来并不意味着线程就开始运行了
覆写run方法是给线程指令清单
但是start方法,则是让线程去真正的执行

方法一:继承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接口
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类,使用匿名内部类
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接口,使用匿名内部类
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表达式
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;


public class ThreadDemo6 {
    public static void main(String[] args)throws ExecutionException, InterruptedException {
        // 创建 Callable 实例
        MyCallable callable = new MyCallable();
        // 用于接收 Callable 结果的对象
        FutureTask futureTask = new FutureTask(callable);
        // 创建新线程
        Thread thread = new Thread(futureTask);
        // 启动线程
        thread.start();
        // 接收新线程执行的结果
        int result = futureTask.get();
        System.out.println(Thread.currentThread().getName() +
                "——新线程返回的结果为:" + result);
    }
}

class MyCallable implements Callable {
    @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();
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/785036.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号