线程实现

创建线程

三种方式创建线程

  • 继承Thread
  • 实现Runnable
  • 实现Callable接口
    • 有缓存
    • 结果可能需要等待,会阻塞!
public class ThreadNew {
    public static void main(String[] args) {
        // 1.继承Thread类
        new MyThread1().start();

        // 2.实现Runnable接口
        new Thread(new MyThread2()).start();

        // 3.实现Callable接口
        FutureTask<Integer> futureTask = new FutureTask<>(new MyThread3());
        new Thread(futureTask).start();
        try {
            System.out.println(futureTask.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}

// 1.继承Thread类
class MyThread1 extends Thread {
    @Override
    public void run() {
        System.out.println("MyThread1");
    }
}

// 2.实现Runnable接口
class MyThread2 implements Runnable {
    @Override
    public void run() {
        System.out.println("MyThread2");
    }
}

// 3.实现Callable接口
class MyThread3 implements Callable<Integer> {
    @Override
    public Integer call() throws Exception {
        System.out.println("MyThread3");
        return 100;
    }
}

通过线程池创建线程

Runable方式

public class TestPool {
    public static void main(String[] args) {
        // 1.创建服务,创建线程池
        // newFixedThreadPool 参数为: 线程池大小
        ExecutorService service = Executors.newFixedThreadPool(10);

        // 2.执行
        service.execute(new MyThread());
        service.execute(new MyThread());
        service.execute(new MyThread());
        service.execute(new MyThread());

        // 3.关闭连接
        service.shutdown();
    }
}

class MyThread implements Runnable {
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
    }
}

Callable方式

public class TestPool1 {
    public static void main(String[] args) {
        // 1.创建服务,创建线程池
        // newFixedThreadPool 参数为: 线程池大小
        ExecutorService service = Executors.newFixedThreadPool(10);

        // 2.提交执行
        Future<Integer> r1 = service.submit(new MyThread4());
        Future<Integer> r2 = service.submit(new MyThread4());
        Future<Integer> r3 = service.submit(new MyThread4());
        Future<Integer> r4 = service.submit(new MyThread4());

        // 3.获取结果
        try {
            System.out.println(r1.get());
            System.out.println(r2.get());
            System.out.println(r3.get());
            System.out.println(r4.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        // 4.关闭连接
        service.shutdown();
    }
}

class MyThread4 implements Callable<Integer> {
    @Override
    public Integer call() {
        System.out.println(Thread.currentThread().getName());
        return 200;
    }
}
posted @   不写代码想写诗的虫子  阅读(19)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
点击右上角即可分享
微信分享提示