创建线程方法

本文详细介绍了Java中的MyThread、Runnable、Callable接口以及ThreadPoolExecutor的使用,展示了如何通过lambda表达式创建线程和FutureTask,并演示了如何在自定义线程池中并发执行任务。
摘要由CSDN通过智能技术生成
public class MyThread extends Thread {

    @Override
    public void run() {
        System.out.println("my thread");
    }

//    public static void main(String[] args) {
//        MyThread myThread = new MyThread();
//        myThread.start();
//    }
    
    public static void main(String[] args) {
        // 也支持 lambda 表达式
        new Thread(() -> {
            System.out.println("my runnable11");
        }).start();
    }
}
public class MyRunnable implements Runnable {
    @Override
    public void run() {
        System.out.println("my runnable1");
    }

//    public static void main(String[] args) {
//        Thread thread = new Thread(new MyRunnable());
//        thread.start();
//
//        // 也支持 lambda 表达式
//        Thread thread1 = new Thread(() -> {
//            System.out.println("my runnable2");
//        });
//        thread1.start();
//    }

    public static void main(String[] args) {
        // 也支持 lambda 表达式
        new Thread(() -> {
            System.out.println("my runnable3");
        }).start();
    }
}
public class MyCallable implements Callable<Integer> {
    private int num;

    public MyCallable(int num) {
        this.num = num;
    }

    @Override
    public Integer call() throws Exception {
        int sum = 0;
        for (int i = 1; i <= num; i++) {
            sum += i;
        }
        return sum;
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        MyCallable myCallable = new MyCallable(100);
        FutureTask<Integer> futureTask = new FutureTask<>(myCallable);
        Thread thread = new Thread(futureTask);
        thread.start();
        int result = futureTask.get();
        System.out.println("1到100的和为:" + result);
    }
}
public class TempThread implements Runnable{
    @Override
    public void run() {
        // 打印正在执行的缓存线程信息
        System.out.println(Thread.currentThread().getName() + "正在被执行");
        try {
            // sleep一秒保证3个任务在分别在3个线程上执行
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}


public class TestThreadPoolExecutor {
    public static void main(String[] args) {
        // 创建数组型缓冲等待队列
        BlockingQueue<Runnable> bq = new ArrayBlockingQueue<Runnable>(10);
        // ThreadPoolExecutor:创建自定义线程池,池中保存的线程数为3,允许最大的线程数为6
        //非核心线程空闲后可以存活的时间,时间单位,阻塞队列,线程工厂
        ThreadPoolExecutor tpe =
                new ThreadPoolExecutor(
                        3,
                        6,
                        50,
                        TimeUnit.MILLISECONDS, bq);

        // 创建3个任务
        Runnable t1 = new TempThread();
        Runnable t2 = new TempThread();
        Runnable t3 = new TempThread();

        // 3个任务在分别在3个线程上执行
        tpe.execute(t1);
        tpe.execute(t2);
        tpe.execute(t3);

        // 关闭自定义线程池
        tpe.shutdown();
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值