RT
General simple counter implementation ~
Bored and casually written...
1. Caller
Package time;/*** @ author Andy */public class loginservice {public void login () {timermanager. logintime. incr ();} public void logout () {timermanager. logouttime. incr ();} public static void main (string [] ARGs) {loginservice L = new loginservice (); L. login (); L. logout ();}}
Counter manager
Package time;/*** @ author Andy */public class timermanager {public static timer logintime = new timer (2, new printtimerprocessor ()); public static timer logouttime = new timer (4, new printtimerprocessor (); static {logintime. start (); logouttime. start ();}}
Counter thread performer/counting end operator (currently only printed on the console)
Package time;/*** @ author Andy */public class timer {private itimeprocessor Dao = NULL; private int T = 1; Public timer (int t, itimeprocessor Dao) {This. T = T; this. dao = Dao;} public static timer; private long counter = 0; Public void incr () {counter ++;} private long getcount () {return counter ;} public void clear () {counter = 0;} public void start () {runnable r = new runnable () {@ overridepublic void run () {While (true) {try {Dao. process (getcount (); thread. sleep (T * 1000);} catch (exception e) {e. printstacktrace () ;}}}; thread = new thread (r); thread. start (); system. out. println ("thread OK") ;}} interface itimeprocessor {public void process (Long Count);} class printtimerprocessor implements itimeprocessor {public void process (Long Count) {system. out. println (count );}}