service

package com.jaykid.hsbc.monitoring.service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class InputDeamon extends Thread {

	MonitoringService MonitoringService;
	
	public InputDeamon() {
		MonitoringService  = new MonitoringServiceImpl();
		MonitoringService.init();
	}

	@Override
	public void run() {

		String input = "";
		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
		while (!("end".equalsIgnoreCase(input))) {
			try {
				input = bufferedReader.readLine();
//				System.out.println(input);
				parseInput(input);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	private void regist(String key) {
		MonitoringService.register(key);
//		System.out.println("InputDeamon input");
	}

	private void update(String key) {
		MonitoringService.update(key);
//		System.out.println("InputDeamon update");
	}

	private void parseInput(String input) {
		String[] inputArray = input.split("-");
//		System.out.println(inputArray.length);
		if (inputArray.length < 2) {
			return;
		} else if ("r".equalsIgnoreCase(inputArray[0])) {
			regist(inputArray[1]);
		} else if ("u".equalsIgnoreCase(inputArray[0])) {
			update(inputArray[1]);
		}

	}
}

 

 

package com.jaykid.hsbc.monitoring.service;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class MonitoringServiceImpl implements MonitoringService {

	private ScheduledExecutorService scheduledExecutorService;

	private ConcurrentHashMap<String, Object> concurrentHashMap;

	private static final long TIME_OUT_VALUE = 3000;

	public class TimeOutAction implements Runnable {

		String id;

		public TimeOutAction(String id) {
			this.id = id;
		}

		@Override
		public void run() {
			if (concurrentHashMap.get(id) == null) {
				System.out.println("Status-updated:" + id);
			} else {
				System.out.println("Status-TimeOut:" + id);
			}
		}

	}

	@Override
	public void register(Object obj) {
		checkType(obj);
		scheduledExecutorService.schedule(new TimeOutAction((String) obj), TIME_OUT_VALUE, TimeUnit.MILLISECONDS);
		concurrentHashMap.put((String) obj, obj);
		System.out.println("Regist:"  + (String) obj);
	}

	@Override
	public void update(Object obj) {
		checkType(obj);
		concurrentHashMap.remove((String) obj);
		System.out.println("Update:"  + (String) obj);
	}

	private void checkType(Object obj) {
		if (!(obj instanceof String)) {
			throw new RuntimeException("Key is not String");
		}
	}

	@Override
	public void init() {
		concurrentHashMap = new ConcurrentHashMap<String, Object>();
		scheduledExecutorService = Executors.newScheduledThreadPool(1);
	}

	@Override
	public void start() {
		// TODO Auto-generated method stub

	}

	@Override
	public void stop() {
		// TODO Auto-generated method stub

	}

	@Override
	public void depose() {
		// TODO Auto-generated method stub

	}

}

 

你可能感兴趣的:(java,thread)