多线程有几种实现方式实操

多线程有几种实现方式?

1、继承Thread

2、实现Runnable

3、实现接口通过FutureTask包装器来创建Thread线程

4、通过线程池创建线程,使用线程池接口实现有返回结果的多线程

 

1、继承Thread

package com.stardust.test;

public class ThreadTest {

    public static void main(String[] args) throws Exception {
        Thread thread = new MyThread();
        thread.start();
        Thread.sleep(5000);//睡5秒
        System.out.println("主线程执行完毕");
    }
}

class MyThread extends Thread {
    @Override
    public void run() {
        String name = Thread.currentThread().getName();
        System.out.println("开启了一个线程线程名称为:" + name);
    }
}

2、实现Runnable

package com.stardust.test;

public class RunnableTest {
    public static void main(String[] args) throws Exception {
        Runnable runnable = new MyRunnable2();
        new Thread(runnable).start();
        Thread.sleep(5000);//睡5秒
        System.out.println("主线程执行完毕");
    }
}


class MyRunnable implements Runnable {
    @Override
    public void run() {
        String name = Thread.currentThread().getName();
        System.out.println("开启了一个线程线程名称为:" + name);
    }
}

3、实现接口通过FutureTask包装器来创建Thread线程

package com.stardust.test;

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

public class CallableTest {
    public static void main(String[] args) throws Exception {
        FutureTask futureTask = new FutureTask(new MyCallable());
        new Thread(futureTask).start();
        Thread.sleep(5000);//睡5秒
        System.out.println("主线程执行完毕");
    }
}

class MyCallable implements Callable {

    @Override
    public Object call() throws Exception {
        String name = Thread.currentThread().getName();
        System.out.println("开启了一个线程线程名称为:" + name);
        return null;
    }
}

4、通过线程池创建线程,使用线程池接口实现有返回结果的多线程

ExecutorService详细介绍

package com.stardust.test;

import java.util.concurrent.*;

public class ExecutorServiceTest {
    public static void main(String[] args) throws Exception {
        ExecutorService pool1 = Executors.newFixedThreadPool(1);//创建大小为1的线程池
        System.out.println("测试runnable");
        pool1.execute(new MyRunnable());
        Thread.sleep(3000);//睡5秒
        pool1.shutdown();//关闭线程池

        System.out.println("测试callable以及返回值");
        ExecutorService pool2 = Executors.newFixedThreadPool(1);//创建大小为1的线程池
        Future future = pool2.submit(new MyCallable2());
        Thread.sleep(3000);
        System.out.println("线程返回值为:" + future.get());
        pool2.shutdown();//关闭线程池
        System.out.println("主线程执行完毕");
    }
}


class MyRunnable2 implements Runnable {
    @Override
    public void run() {
        String name = Thread.currentThread().getName();
        System.out.println("开启一个runnable:" + name);
    }
}

class MyCallable2 implements Callable {

    @Override
    public Object call() throws Exception {
        String name = Thread.currentThread().getName();
        System.out.println("开启一个callable:" + name);
        return Thread.currentThread().getName();
    }
}



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值