java 创建线程

1. java创建多线程,异步流程处理

 //开新线程开    构造函数传参   也可是内部类
    public class startThread implements Runnable {
    	String day = "";
        String from = "";
        String end = "";

        public startThread(String day, String from, String end) {
           this.day = day;
           this.from = from;
           this.end = end;
        }

        @Override
        public void run() {
            try {
               // 业务操作
            } catch (Exception e) {
                try {
                    StringWriter sw = new StringWriter();
                    PrintWriter pw = new PrintWriter(sw);
                    // 将出错的栈信息输出到printWriter中
                    e.printStackTrace(pw);
                    pw.flush();
                    sw.flush();
                    sout("计算功能异常:\r\n"+sw.toString());
                    sw.close();
                    pw.close();
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            }
        }
    }


// 需要开启 新线程的地方
new Thread(new startThread(day,from,end)).start();

2. java创建带返回值的多线程

package com.yeahmobi.dsp.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.*;

public class WithReturnThread {

    public static void main(String[] args) throws ExecutionException,
            InterruptedException {
        System.out.println("----程序开始运行----");
        Date date1 = new Date();

        int taskSize = 5;
        // 创建一个线程池
        ExecutorService pool = Executors.newFixedThreadPool(taskSize);
        // 创建多个有返回值的任务
        List<Future> list = new ArrayList<Future>();
        for (int i = 0; i < taskSize; i++) {
            Callable c = new MyCallable(i + " ");
            // 执行任务并获取Future对象
            Future f = pool.submit(c);
            // System.out.println(">>>" + f.get().toString());
            list.add(f);
        }
        // 关闭线程池
        pool.shutdown();

        // 获取所有并发任务的运行结果
        for (Future f : list) {
            // 从Future对象上获取任务的返回值,并输出到控制台
            System.out.println(">>>" + f.get().toString());
        }
        Date date2 = new Date();
        System.out.println("----程序结束运行----,程序运行时间【"
                + (date2.getTime() - date1.getTime()) + "毫秒】");
    }
}

class MyCallable implements Callable<Object> {
        private String sql;
        private ClickhouseDao clickhouseDao;

        public MyCallable(String sql, ClickhouseDao clickhouseDao) {
            this.sql = sql;
            this.clickhouseDao = clickhouseDao;
        }

        public MyCallable(String sql) {
            this.sql = sql;
        }

        public Object call() throws Exception {

            JSONArray query = clickhouseDao.query(sql.toString());
            if (query.size() > 0) {
                return query.get(0);
            } else {
                return new JSONObject();
            }
       }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

base 西安内推私信

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

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

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

打赏作者

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

抵扣说明:

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

余额充值