java多线程

java多线程实现得方式(第一种:继承Thread;第二种:实现Runable 接口;第三种:实现Callable接口通过FutureTask包装器来创建Thread线程;第四种:使用ExecutorServiceCallableFuture实现有返回结果的多线程

一.继承Thread

/**
 * @ClassName :   Thread
 * @Description: 第一种实现(继承Thread类)
 * @Author: 13394
 * @CreateDate: 2018/10/3 14:44
 * @Version: 1.0
 */
public class MyThread extends Thread{
    @Override
    public void run() {
        System.out.println("你是猪啊!!!!!!!!!!!!!!");
    }

    @Test
    public void test() throws Exception{
          Class clazz=  Class.forName("com.bootdo.common.config.collection.test.dxc.MyThread");
          MyThread object= (MyThread)clazz.newInstance();
          object.start();
    }

}

二.实现Runable 接口

/**
 * @ClassName :   MyRunable
 * @Description: 第二种实现(实现Runnable接口)
 * @Author: 13394
 * @CreateDate: 2018/10/3 17:01
 * @Version: 1.0
 */
public class MyRunable implements  Runnable {
    @Override
    public void run() {
        System.out.println("你是一条狗!!!!!!!!!!!!!!11");
    }
    @Test
    public void test() throws Exception{
       Class clazz= Class.forName("com.bootdo.common.config.collection.test.dxc.MyRunable");
       MyRunable object=(MyRunable)clazz.newInstance();
       Thread thread=new Thread(object); //实例化Thread 并且传入自己的MyRunable        thread.start();
    }
}

三.实现Callable接口通过FutureTask包装器来创建Thread线程

/**
 * @ClassName :   MyCallable
 * @Description: 实现Callable接口通过FutureTask包装器来创建Thread线程
 * @Author: 13394
 * @CreateDate: 2018/10/3 17:10
 * @Version: 1.0
 */
public class MyCallable<V> implements Callable<V> {


    @Override
    public V call() throws Exception {

        return null;
    }

    @Test
    public void test(){
        Callable<V> callable=new MyCallable<V>();
        FutureTask<V> futureTask=new FutureTask<V>(callable);
        Thread thread=new Thread();
        thread.start();
    }
}

四.使用ExecutorServiceCallableFuture实现有返回结果的多线程

/**
 * @ClassName :   MyExecutorServiceCallableFuture
 * @Description: 使用ExecutorServiceCallableFuture实现有返回结果的多线程
 * @Author: 13394
 * @CreateDate: 2018/10/3 17:18
 * @Version: 1.0
 */
@RunWith(Parameterized.class)
public class MyExecutorServiceCallableFuture implements Callable<Object> {
    private String taskNum;

    public MyExecutorServiceCallableFuture(String taskNum) {
        this.taskNum = taskNum;
    }

    @Override
    public Object call() throws Exception {
        System.out.println(">>>" + taskNum + "任务启动");
        Date dateTmp1 = new Date();
        Thread.sleep(1000);
        Date dateTmp2 = new Date();
        long time = dateTmp2.getTime() - dateTmp1.getTime();
        System.out.println(">>>" + taskNum + "任务终止");
        return taskNum + "任务返回运行结果,当前任务时间【" + time + "毫秒】";
    }


}
public class Test {

    @org.junit.Test
    public void test() throws Exception {
        System.out.println("----程序开始运行----");
        Date date1 = new Date();
        int taskSize = 5;
        // 创建一个线程池
        ExecutorService executorService= Executors.newFixedThreadPool(taskSize);
        List<Future> list=new ArrayList<Future>();
        for (int i = 0; i < taskSize; i++) {
            Callable callable=new MyExecutorServiceCallableFuture(i+" ");
            Future future=  executorService.submit(callable);
            list.add(future);
        }
        executorService.shutdown();
        for(Future f:list){
            System.out.println(">>>" + f.get().toString());
        }
        Date date2=new Date();
        System.out.println("----程序结束运行----,程序运行时间【"+(date2.getTime()-date1.getTime())+"毫秒】");

    }
}

 

转载于:https://my.oschina.net/u/3653755/blog/2222797

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值