锘??xml version="1.0" encoding="utf-8" standalone="yes"?>综合久久国产,成年人在线视频免费观看,成人区精品一区二区婷婷 http://www.aygfsteel.com/feuyeux/category/47048.htmlBelong to Eric Han zh-cn Wed, 25 Apr 2012 20:52:01 GMT Wed, 25 Apr 2012 20:52:01 GMT 60 杞瀹㈡埛绔殑瀹炵幇 http://www.aygfsteel.com/feuyeux/archive/2011/07/08/353948.htmlLu Han Lu Han Fri, 08 Jul 2011 08:01:00 GMT http://www.aygfsteel.com/feuyeux/archive/2011/07/08/353948.html http://www.aygfsteel.com/feuyeux/comments/353948.html http://www.aygfsteel.com/feuyeux/archive/2011/07/08/353948.html#Feedback 1 http://www.aygfsteel.com/feuyeux/comments/commentRss/353948.html http://www.aygfsteel.com/feuyeux/services/trackbacks/353948.html Scenario 鏈嶅姟鍣?:瀹㈡埛绔痭 鍙戦丯otification緇欏鎴風鐨?/span>鍚?/span>澶勭悊 鏈嶅姟鍣ㄧ緇欑涓涓鎴風鍙戦乶otification錛岀劧鍚庡湪闄愬畾鏃墮棿鍐咃紝絳夊緟瀹㈡埛绔綔鍑哄洖搴?#8212;鍚戞湇鍔″櫒鍙戦乺equest銆傚鏋滃鎴風涓鐩存病鏈夊洖澶嶏紝鏈嶅姟鍣ㄤ細鍦ㄥ埌杈鵑檺瀹氭椂闂村悗錛屽悜絎簩涓鎴風鍙戦乶otification銆傚鏋滃鎴風鍦ㄩ檺瀹氭椂闂村唴鍥炲錛屾湇鍔″櫒绔斁寮冨啀緇欏叾浠栧鎴風鍙戦佹秷鎭?/span>Design 鏈嶅姟鍣ㄣ佸鎴風浣跨敤socket鍙戦佸拰鎺ユ敹淇℃伅 鍙戦佺瀛樺湪涓涓鎴風鍒楄〃錛屾瘡嬈″彂閫佷竴緇欎竴涓鎴風錛屽彂閫佸悗錛屽悜Helper鍙戦佹坊鍔犺瀹㈡埛绔痠d鐨勮姹傘?br /> 瀹㈡埛绔敹鍒頒俊鎭細鍚慔elper鍙戦佸垹闄よid鐨勮姹傘?br /> Helper鏀跺埌add鏃?/span>錛屽惎鍔ㄤ竴涓?/span>ScheduledExecuto rService綾葷殑 schedule錛?/span> 寤舵椂鍚姩 涓涓嚎紼嬶紝騫跺皢璇?/span>schedule緙撳瓨 銆俽emove鏃訛紝浠庣紦瀛橀噷鍙栧嚭 schedule騫跺仠姝㈠畠銆傚鏋滃湪寤舵椂鏃墮棿鍐咃紝綰跨▼娌℃湁琚仠姝紝瀹冧細琚墽琛岋細浠庣紦瀛樹腑鍙栧嚭錛屽憡璇夋湇鍔″櫒鍚戜笅涓涓鎴風鍙戦佽姹傘?/span> UML
Code
Server public class Server { public static void main(String[] args) throws Exception { final String id = " 100 " ; ServerSocket serverSocket = new ServerSocket(IO.BIO_TCP_PORT); System.out.println( " Server is listening on port: " + IO.BIO_TCP_PORT); Socket socket = null ; try { socket = serverSocket.accept(); } catch (Exception e) { System.out.println( " accept socket error. " ); } SendingNotification sender = new SendingNotification(id, socket); sender.start(); ReceivingRequest receiver = new ReceivingRequest(socket); receiver.start(); } SendingNotification public class SendingNotification extends Thread { private String id; private Socket socket; public SendingNotification(String sdId, Socket socket) { this .id = sdId; this .socket = socket; } @Override public void run() { Helper.getInstance().add(id); OutputStream outputStream = null ; byte [] buffer = new byte [ 1024 ]; try { outputStream = socket.getOutputStream(); buffer = (id + " \n " ).getBytes(); outputStream.write(buffer); outputStream.flush(); } catch (Exception e) { System.out.println( " don't send success " ); try { outputStream.close(); socket.close(); } catch (Exception e1) { } } } } ReceivingRequest public class ReceivingRequest extends Thread { private Socket socket; public ReceivingRequest(Socket socket) { this .socket = socket; } @Override public void run() { BufferedReader in; boolean finished = false ; while ( ! finished) { try { in = new BufferedReader( new InputStreamReader(socket.getInputStream())); String line = in.readLine(); if (line == null ) { Thread.sleep( 100 ); continue ; } Helper.getInstance().remove(line); in.close(); socket.close(); finished = true ; } catch (Exception e) { System.out.println( " receive fails to run. " ); } } } } Helper public class Helper { private static Helper instance = new Helper(); private ConcurrentHashMap < String, Schedule > cache = new ConcurrentHashMap < String, Schedule > (); private int timeout = 10 ; public static Helper getInstance() { return instance; } private Schedule addTask( final String id) { final Schedule schedule = new Schedule(); schedule.schedule( new Runnable() { public void run() { doNext(id); schedule.shutdown(); } }, timeout, SECONDS); return schedule; } private void doNext(String id) { Schedule schedule = cache.remove(id); System.out.println( " time out and do next well. " ); System.out.println( " total time= " + schedule.getSeconds()); } public void add( final String id) { Schedule schedule = addTask(id); cache.put(id, schedule); System.out.println( " Add to cache successfully " ); } public void remove( final String id) { Schedule schedule = cache.remove(id); if (schedule == null ) System.out.println( " no schedule exist. " ); else { schedule.shutdown(); System.out.println( " Remove to cache successfully " ); } } Schedule public class Schedule { ScheduledExecutorService excutor; private long startTime; public Schedule() { excutor = Executors.newSingleThreadScheduledExecutor(); startTime = System.currentTimeMillis(); } public long getTotalTime() { long endTime = System.currentTimeMillis(); return endTime - startTime; } public String getSeconds() { long s = getTotalTime() / 1000 ; return s + " seconds " ; } public void schedule(Runnable command, long delay, TimeUnit unit) { excutor.schedule(command, delay, unit); } public void shutdown() { excutor.shutdownNow(); } } Client public class Client { public static void main(String[] args) { Socket socket; try { socket = new Socket(IO.SERVER_IP, IO.BIO_TCP_PORT); readLine(socket); } catch (UnknownHostException e) { } catch (IOException e) { } catch (InterruptedException e) { } } private static void readLine(Socket socket) throws IOException, InterruptedException { BufferedReader in = new BufferedReader( new InputStreamReader(socket.getInputStream())); PrintWriter out = new PrintWriter(socket.getOutputStream(), true ); boolean flag = true ; while (flag) { String command = in.readLine(); if (command == null ) { flag = true ; continue ; } else { // Thread.sleep(2000); out.println(command); out.flush(); out.close(); in.close(); socket.close(); flag = false ; } } } IO public interface IO { String SERVER_IP = " 127.0.0.1 " ; // "192.168.225.166"; int BIO_TCP_PORT = 9109 ;
]]>
主站蜘蛛池模板:
钟山县 |
油尖旺区 |
正蓝旗 |
三门县 |
交口县 |
辉县市 |
康保县 |
阳江市 |
深泽县 |
林州市 |
凤庆县 |
清水河县 |
华宁县 |
万全县 |
嘉善县 |
林州市 |
永和县 |
平顺县 |
九龙县 |
华蓥市 |
柳江县 |
宜昌市 |
临邑县 |
临海市 |
平乡县 |
霍城县 |
东莞市 |
鄱阳县 |
都昌县 |
张家川 |
邻水 |
房山区 |
禹城市 |
孟连 |
淮南市 |
陆良县 |
彩票 |
京山县 |
中江县 |
永平县 |
怀来县 |