KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > async > Math


1 /**************************************************************************************
2  * Copyright (c) Jonas Bon?r, Alexandre Vasseur. All rights reserved. *
3  * http://aspectwerkz.codehaus.org *
4  * ---------------------------------------------------------------------------------- *
5  * The software in this package is published under the terms of the LGPL license *
6  * a copy of which has been included with this distribution in the license.txt file. *
7  **************************************************************************************/

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 JavaDoc(a), new Integer JavaDoc(b), new Integer JavaDoc(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 JavaDoc(a), new Integer JavaDoc(b), new Integer JavaDoc(a - b)
35         );
36     }
37
38     public static void main(String JavaDoc args[]) throws Throwable JavaDoc {
39         Math JavaDoc math = new Math JavaDoc();
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