• 浅谈线程runnable和callable的使用及区别


    线程使用比较广泛,但实际上一般项目很少用上线程,线程常用于优化复杂的程序执行流程,把一些与业务关系关系不大但是必须要执行的流程使用线程的方式让子线程去执行,主流程只返回跟业务有关的信息

    runnable是无返回值的执行线程;callable是有返回值的执行线程

    实现runable接口的实现类通常使用execute来开启线程池中的线程来执行任务,但是也支持submit来开启原则上不会报错,因为实现类返回结果就是void

    实现callable接口的实现类只能用submit来开启线程池中的线程来执行任务

    下面是测试代码:

    public class TestThread {
    
        private static int nThreads = Runtime.getRuntime().availableProcessors() * 2 + 1;
    
        private static ExecutorService executors = Executors.newFixedThreadPool(nThreads, new ThreadFactory() {
    
            private final String threadNamePrefix = "si_query_task_";
    
            private final AtomicInteger count = new AtomicInteger(1);
    
            @Override
            public Thread newThread(Runnable r) {
                Thread t = new Thread(Thread.currentThread().getThreadGroup(), r, threadNamePrefix + count.getAndIncrement());
                t.setDaemon(true);
                return t;
            }
        });
    
        public static void main(String[] args) {
    
            List<Future<String[]>> fList = new ArrayList<>();
    
            for (int i = 0; i < 10; i++) {
                final int nextInt = new Random().nextInt(100);
                Future<String[]> f = executors.submit(new TestTaskcb(nextInt));
                fList.add(f);
            }
    
            for (Future<String[]> future : fList) {
                try {
                    String[] result = future.get();
                   // System.out.println(result[0] + " , 结果   " + result[1]);
                } catch (InterruptedException e) {
                } catch (ExecutionException e) {
                }
            }
            System.out.println("call线程结束");
            
            for (int i = 0; i < 10; i++) {
                final int nextInt = new Random().nextInt(100);
                executors.execute(new TestTaskvoid(nextInt));
            }
            System.out.println("void线程结束");
            
            System.out.println("main 线程结束 ");
    
        }
    
        static class TestTaskcb implements Callable<String[]> {
    
            private int i;
    
            public TestTaskcb(int i) {
                this.i = i;
            }
    
            @Override
            public String[] call() throws Exception {
                String result = i % 2 == 0 ? "S" : "F";
                // 业务处理逻辑
                //Thread.sleep(1000);
                System.out.println(Thread.currentThread().getName() + "第" + i + "次任务");
                return new String[]{ Thread.currentThread().getName(), result };
            }
        }
        
        static class TestTaskvoid implements Runnable {
    
            private int i;
    
            public TestTaskvoid(int i) {
                this.i = i;
            }
    
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + "第" + i + "次任务");
            }
        }
    }

    线程执行是随机执行的,打印结果不可作参考

  • 相关阅读:
    安卓开发学习日记 DAY1
    安卓开发学习日记 DAY5——监听事件onClick的实现方法
    安卓开发学习日记 DAY3——TextView,EditView,ImageView
    [原] XAF Try an XAF application that uses the Entity Framework as an ORM layer
    [原] XAF 如何使用Top
    [原] Sql Server 获取某年某月有多少个工作日(仅不包含星期日)
    [原] XAF Split View (aka MasterDetailMode=ListViewAndDetailView) improvements
    Sql Server 问题之between and 使用注意事项
    [原] XAF ListView 粘贴行或单元格
    [原] XAF How to see and edit the time part in the DatePropertyEditor for the System.DateTime property
  • 原文地址:https://www.cnblogs.com/zyf-yxm/p/9959198.html
Copyright © 2020-2023  润新知