如何改写单例模式?在连接请求这一块儿出现了多次重复连接,需要改为单例模式。

如题:

 class InitSocketThread extends Thread {
        public void run() {
            super.run();
            try {
                initSocket();
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
}

求大神帮助啊,急求!!!!


长风秋雁
浏览 766回答 1
1回答

RISEBY

class MyThreadScopeData{//将类变成单例的private MyThreadScopeData(){}public static MyThreadScopeData getThreadInstance(){MyThreadScopeData instance = map.get();if(instance==null){instance = new MyThreadScopeData();map.set(instance);}return instance;}private static ThreadLocal map = new ThreadLocal();public void initSocket(){initSocket();}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java