Method call
Consider a log scenario.
Multiple programs call a MyLog class to generate log objects.
Log objects are actually output to a large device.
The MyLog class is required to use the singleton mode. A maximum of two instances are allowed. Call getInstance () to obtain an instance. Each instance has a busy or free status, indicating whether it is idle. If all instances are busy, a null pointer is returned.
Class MyLog {private static MyLog a = new MyLog (); private static MyLog B = new MyLog (); private boolean busy; private MyLog () {} public void use () {busy = true;} public void free () {busy = false;} public static MyLog getInstance () {if (. busy = false) return a; if (B. busy = false) return B; return null;} public class MyTest {public static void main (String [] args) {MyLog t1 = MyLog. getInstance (); t1.use (); MyLog t2 = MyLog. getInstance (); t2.use (); MyLog t3 = MyLog. getInstance (); System. out. println (t3); t2.free (); t3 = MyLog. getInstance (); System. out. println (t3 );}}