JAVA《多线程多人上线通知案例》

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
代理类的两种写法:
package com.wangbiao.mybetty.demo;
 
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
 
public class PlayerProxy  implements InvocationHandler {
 
 
//    public Object getPlayer(Object target) {
//        return Proxy.newProxyInstance(
//                target.getClass().getClassLoader(),
//                target.getClass().getInterfaces(),
//                // 这里其实是要实现jdk代理InvocationHandler的接口,然后改成JKD8的写法了而已
//                (proxy, method, args) -> {
//                    System.out.println("[JDK动态代理]开着法拉利到小区接你");
//                    // 执行目标对象方法
//                    Object returnValue = method.invoke(target, args);
//                    System.out.println("[JDK动态代理]开着法拉利送你回家");
//                    return returnValue;
//                }
//        );
//    }
 
     
     
     
     
    //真实对象
    private Object target;
 
    /**
     * 建立代理对象和真实对象的代理关系方法,并返回代理对象
     * @param target 真实对象
     * @return 代理对象
     */
    public Object bing(Object target){
        this.target = target;
        return Proxy.newProxyInstance(target.getClass().getClassLoader(),target.getClass().getInterfaces(),this);
    }
 
    /**
     *
     * @param proxy 代理对象
     *
     * @param method 当前调度方法
     * @param args 当前方法参数
     * @return 代理结果返回
     * @throws Throwable 异常
     */
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("进入代理逻辑方法");
        System.out.println("在调度真实对象之前的服务");
        Object obj = method.invoke(target, args);//相当于调用sayHello方法
 
        System.out.println("在调度真实对象后的方法");
        return obj;
    }
 
 
}

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.wangbiao.palyermanager;
import com.wangbiao.player.Player;
 
/**
 * TODO * * @author wangbiao * @Title TODO * @module TODO * @description 多人在线管理器 * @date 2021/4/19 13:26
 */
public interface PlayerManager {
    /**
     * 增加一个玩家对象。
     */
    void addPlayer(Player player) throws InterruptedException;
 
    /**
     * 根据用户名获取玩家对象。
     */
    Player getPlayer(String username);
 
    /**
     * 向系统中的所有玩家广播一条消息。
     */
    void broadcast(String message) throws InterruptedException;
}

 

 

 

 

posted @   余生请多指教ANT  阅读(403)  评论(5编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示