1 8 package examples.async; 9 10 import is.Async; 11 import is.Service; 12 13 @Service 14 public class Math { 15 16 static { 17 System.out.println("Math.'static initializer'"); 18 } 19 20 @Async(timeout = 10) 21 public void add(int a, int b) { 22 System.out.printf( 23 "[ %s ] %d + %d = %d\n", 24 Thread.currentThread().getName(), 25 new Integer (a), new Integer (b), new Integer (a + b) 26 ); 27 } 28 29 @Async(timeout = 2) 30 public void subtract(int a, int b) { 31 System.out.printf( 32 "[ %s ] %d - %d = %d\n", 33 Thread.currentThread().getName(), 34 new Integer (a), new Integer (b), new Integer (a - b) 35 ); 36 } 37 38 public static void main(String args[]) throws Throwable { 39 Math math = new Math (); 40 System.out.println("\n================ Async sample ================="); 41 42 math.add(5, 4); 43 math.add(1, 5); 44 math.add(2, 6); 45 math.add(4, 4); 46 math.add(8, 4); 47 math.subtract(7, 4); 48 math.subtract(3, 5); 49 math.subtract(1, 6); 50 math.subtract(4, 4); 51 math.subtract(8, 4); 52 Thread.sleep(100); 53 System.exit(0); 54 } 55 } 56 | Popular Tags |