Java并发编程:Callable、Future和FutureTask 实现龟兔赛跑

1、不清楚的看博客http://www.cnblogs.com/dolphin0520/p/3949310.html

我们使用上面的代码来实现一个龟兔赛跑

复制代码
package com.weiyuan.test;

import java.util.concurrent.Callable;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class Main {

    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        int countTime = 4000;//跑步的总时间
        ExecutorService service = Executors.newFixedThreadPool(2);
        Task tuzi = new Task("兔子", 500);
        Task wugui = new Task("乌龟", 1000);
        Future<Integer> rst1 = service.submit(tuzi);
        Future<Integer> rst2 = service.submit(wugui);
        
        /**
         * 跑步总的时间长度是4秒
         * */
        Thread.sleep(countTime);
        
        /*
         * 跑步时间到了,记得退出线程
         * */
        tuzi.setFlag(false);
        wugui.setFlag(false);
        
        Integer num1 = rst1.get();
        Integer num2 = rst2.get();
        System.out.println("兔子"+"跑了"+num1+"步");
        System.out.println("乌龟"+"跑了"+num2+"步");
        
        
        /**
         * 关闭线程池
         * */
        service.shutdown();
    }
    
    
    public static class Task implements Callable<Integer>{

        /**
         * 兔子还是乌龟
         * time 兔子和乌龟跑步的时间
         * 跑步时间大了停止线程的退出
         * 跑步的步数
         * */
        private String name; 
        private long time;
        private boolean flag = true;
        private int step = 0;
        
        
        public Task(String name, long time) {
            this.name = name;
            this.time = time;
        }

        @Override
        public Integer call() throws Exception {
            // TODO Auto-generated method stub
            while(flag){
                Thread.sleep(time);
                step ++ ;
            }
            
            return step;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public long getTime() {
            return time;
        }

        public void setTime(long time) {
            this.time = time;
        }

        public boolean isFlag() {
            return flag;
        }

        public void setFlag(boolean flag) {
            this.flag = flag;
        }
        
        
        
    }

}
复制代码

程序的运行结果是:

兔子跑了9步
乌龟跑了5步

posted on   luzhouxiaoshuai  阅读(209)  评论(0编辑  收藏  举报

编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示