其他分享
首页 > 其他分享> > 实现Callable接口,静态代理模式

实现Callable接口,静态代理模式

作者:互联网

模拟龟兔赛跑的测试代码:

//模拟龟兔赛跑
public class Race implements Runnable {
​
    //胜利者
    private static String winner;
​
    @Override
    public void run() {
        for (int i = 0; i <= 100; i++) {
​
            //模拟兔子休息
            if(Thread.currentThread().getName().equals("兔子") && i%10==0){
                try {
                    Thread.sleep(1/10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
​
            //判断比赛是否结束
            boolean flag = gameOver(i);
            //如果比赛结束了,就停止程序
            if(flag==true){
                break;
            }
​
            System.out.println(Thread.currentThread().getName()+"--->跑了"+i+"步");
        }
    }
​
    //判断是否完成比赛
    private boolean gameOver(int steps){
        //判断是否有胜利者
        if(winner!=null){//已经存在胜利者了
            return true;
        }{
            if(steps >= 100){
                winner = Thread.currentThread().getName();
                System.out.println("winner is " + winner);
                return true;
            }
        }
        return false;
    }
​
    public static void main(String[] args) {
        Race race = new Race();
​
        new Thread(race,"兔子").start();
        new Thread(race,"乌龟").start();
    }
}

 

实现Callable接口

  1. 实现Callable接口,需要返回值类型

  2. 重写call方法,需要抛出异常

  3. 创建目标对象

  4. 创建执行服务:ExecutorService ser = Executors.newFixedThreadPool(1);

  5. 提交执行:Future<Boolean> result1 = ser.submit(t1);

  6. 获取结果:boolean r1 = result1.get();

  7. 关闭服务:ser.shutdownNow();

Callable测试代码:

//线程创建方式三,实现Callable接口
/*
Callable的好处
1.可以定义返回值
2.可以抛出异常
 */
public class TestCallable implements Callable<Boolean> {
​
    private String url; //网络图片地址
    private String name; //保存的文件名
​
    public TestCallable(String url,String name){
        this.url = url;
        this.name = name;
    }
​
    @Override
    public Boolean call() {
        WebDownloader webDownloader = new WebDownloader();
        webDownloader.downloader(url,name);
        System.out.println("下载了文件名为:"+name);
        return true;
    }
​
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        TestCallable t1 = new TestCallable("https://imgessl.kugou.com/uploadpic/softhead/240/20220602/20220602093222410.jpg","1.jpg");
        TestCallable t2 = new TestCallable("https://imgessl.kugou.com/uploadpic/softhead/240/20220602/20220602093222410.jpg","2.jpg");
        TestCallable t3 = new TestCallable("https://imgessl.kugou.com/uploadpic/softhead/240/20220602/20220602093222410.jpg","3.jpg");
​
        //创建执行服务:
        ExecutorService ser = Executors.newFixedThreadPool(3);
​
        //提交执行:
        Future<Boolean> r1 = ser.submit(t1);
        Future<Boolean> r2 = ser.submit(t2);
        Future<Boolean> r3 = ser.submit(t3);
        //获取结果:
        boolean rs1 = r1.get();
        boolean rs2 = r2.get();
        boolean rs3 = r3.get();
​
        System.out.println(rs1);
        System.out.println(rs2);
        System.out.println(rs3);
        //关闭服务:
        ser.shutdownNow();
​
    }
}
​
//下载器
class WebDownloader{
    //下载方法
    public void downloader(String url, String name){
        try {
            FileUtils.copyURLToFile(new URL(url),new File(name));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("IO异常,downloader方法出现问题");
        }
    }
}

 

静态代理模式

静态代理模式测试代码:

//静态代理模式总结:
//真实对象和代理对象都要实现 同一个接口
//代理对象要代理真实角色
    
//好处:
    //代理对象可以做很多真实对象做不了的事情
    //真实对象专注自己的事情
public class StaticProxy {
​
    public static void main(String[] args) {
​
        You you = new You();//你要结婚
        
        
        new Thread( ()-> System.out.println("喜欢你")).start();
​
        new WeddingCompany(new You()).HappyMarry();
​
//        WeddingCompany weddingCompany = new WeddingCompany(you);
//        weddingCompany.HappyMarry();
    }
​
}
​
interface Marry{
​
    void HappyMarry();
}
​
//真实角色
class You implements Marry{
​
    @Override
    public void HappyMarry() {
        System.out.println("要结婚了");
    }
}
​
//代理角色,帮助你结婚
class WeddingCompany implements Marry{
​
    //--->真实目标角色
    private Marry target;
​
    public WeddingCompany(Marry target) {
        this.target = target;
    }
​
    @Override
    public void HappyMarry() {
        before();
        this.target.HappyMarry();//这就是真实对象
        after();
    }
​
    private void after() {
        System.out.println("结婚之后,收尾款");
    }
​
    private void before() {
        System.out.println("结婚之前,布置现场");
    }
}

 

标签:静态,void,System,接口,Callable,println,new,public,out
来源: https://www.cnblogs.com/mhy123/p/16396977.html