多线程三种创建方式

方法一:继承Thread

Thread

package com.bjsxt.create;

/**
 * @author dell
 * @data 2021/3/2
 * 创建一个线程对象,并启动线程
 *
 *    注意:启动main方法,自动创建main线程
 *    
 *      thread.join() 阻塞乌龟线程,乌龟执行完,兔子才有机会
 *      
 *    Thread类的常用方法:
 *     public void run()
 *     thread.start();
 *     this.getName()
 *     this.getPriority()
 *     thread.setNam()
 *     Thread.currentThread().getPriority()
 *
 */
public class TextThread {
    public static void main(String[] args) throws InterruptedException {
        //启动乌龟线程
        Thread thread = new TortoiseThread();
        thread.setName("乌龟1线程");
        thread.start();  //启动一个新的线程
        thread.join(); //阻塞乌龟1线程,其他执行完,乌龟1才有机会

        Thread thread2 = new TortoiseThread();
        thread2.setName("乌龟2线程");
        thread2.start();  //启动一个新的线程


        //兔子也在跑
        Thread.currentThread().setName("主线程");
        while (true){
            System.out.println("兔子领先了" + Thread.currentThread().getName() + " " +Thread.currentThread().getPriority());
        }


    }
}

main

package com.bjsxt.create;

/**
 * @author dell
 * @data 2021/3/2
 * 定义一个乌龟线程类
 */
public class TortoiseThread extends Thread{
    /**
     * 线程体:线程要执行的任务
      */
    @Override
    public void run() {
        while(true){
            System.out.println("乌龟领先" +this.getName()+ " "+this.getPriority());
        }
    }


}

方法二:实现Runnable

Runnable

package com.bjsxt.create2;

/**
 * @author dell
 * @data 2021/3/2
 *      定义线程方式2:  实现Runnable接口
 *      创建线程对象:先定义一个任务,Runnable runnable = new TortoiseRunnable();
 *                 再创建一个线程,Thread thread1 = new Thread(runnable);
 *
 *    •两种方式的优缺点
 *    方式1:继承Thread类
 *    缺点:Java单继承,无法继承其他类
 *    优点:代码稍微简单
 *    方式2:实现Runnable接口
 *    优点  还可以去继承其他类 便于多个线程共享同一个资源
 *    缺点:代码略有繁琐
 *    实际开发中,方式2使用更多一些
 *
 */
public class TortoiseRunnable implements Runnable {
    @Override
    public void run() {

        while (true){
            System.out.println("乌龟领先了" + Thread.currentThread().getName() + " " +Thread.currentThread().getPriority());
        }

    }
}

main

package com.bjsxt.create2;

/**
 * @author dell
 * @data 2021/3/2
 */
public class TextThread {
    public static void main(String[] args) {

        //两个乌龟在跑
        Runnable runnable = new TortoiseRunnable();
        Thread thread1 = new Thread(runnable);
        thread1.setName("乌龟1线程");
        thread1.setPriority(Thread.MAX_PRIORITY);
        thread1.start();

        //Runnable runnable1 = new TortoiseRunnable();
        Thread thread2 = new Thread(runnable,"乌龟2线程");
        thread2.start();


        //一个兔子在跑
        Thread.currentThread().setName("主线程");
        while (true){
            System.out.println("兔子领先了" + Thread.currentThread().getName() + " " +Thread.currentThread().getPriority());
        }

    }
}

优化:使用匿名内部类来创建线程对象

package com.bjsxt.create2;

/**
 * @author dell
 * @data 2021/3/2
 * •可以使用匿名内部类来创建线程对象
 */
public class TextThread2 {
    public static void main(String[] args) {

        //两个乌龟在跑
        Runnable runnable = new Runnable(){
            @Override
            public void run() {
                while (true){
                    System.out.println("乌龟领先了" + Thread.currentThread().getName() + " " +Thread.currentThread().getPriority());
                }
            }
        };


        Thread thread1 = new Thread(runnable);
        thread1.setName("乌龟1线程");
        thread1.setPriority(Thread.MAX_PRIORITY);
        thread1.start();

        Runnable runnable1 = new TortoiseRunnable();
        Thread thread2 = new Thread(runnable,"乌龟2线程");
        thread2.start();


        //一个兔子在跑
        Thread.currentThread().setName("主线程");
        while (true){
            System.out.println("兔子领先了" + Thread.currentThread().getName() + " " +Thread.currentThread().getPriority());
        }

    }
}

方法三:继承Callable接口

优点:有返回值,可以抛出异常

package com.bjsxt.create3;

import java.util.Random;
import java.util.concurrent.*;

/**
 * @author dell         CTRL+z 返回上一步
 * @data 2021/3/2      有返回值,可以抛出异常
 */
public class RandomCallable implements Callable <Integer> {
    @Override
    public Integer call() throws Exception {

        Thread.sleep(6000);
        return new Random().nextInt(10);
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //创建一个线程对象
        Callable<Integer> callable = new RandomCallable();
        FutureTask<Integer> task = new FutureTask(callable);
        Thread thread = new Thread(task);

        //启动线程
        thread.start();

        //获取返回值
        System.out.println(task.isDone());
        int result= task.get();                //得不到返回值就一直等待

        /*
        int result= 0;

        try {
           result = task.get(3, TimeUnit.SECONDS);
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
        */

        System.out.println(task.isDone());
        System.out.println(result);


    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AloneDrifters

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值