Zheng Xu | 5d7cdec | 2014-08-18 17:28:22 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define DO_2_TIMES(x) x x |
| 18 | #define DO_4_TIMES(x) DO_2_TIMES(DO_2_TIMES(x)) |
| 19 | #define DO_16_TIMES(x) DO_4_TIMES(DO_4_TIMES(x)) |
| 20 | #define DO_256_TIMES(x) DO_16_TIMES(DO_16_TIMES(x)) |
| 21 | #define DO_512_TIMES(x) DO_256_TIMES(DO_2_TIMES(x)) |
| 22 | |
| 23 | |
| 24 | public class Main { |
| 25 | public static void main(String[] args) { |
| 26 | Main m = new Main(); |
| 27 | System.out.println(m.foo(-1, -1)); |
| 28 | System.out.println(m.foo(-1, +1)); |
| 29 | System.out.println(m.foo(+1, -1)); |
| 30 | System.out.println(m.foo(+1, +1)); |
| 31 | System.out.println(m.value); |
| 32 | } |
| 33 | |
| 34 | public int foo(int a, int b) { |
| 35 | if ( a >= 0 ) { |
| 36 | if ( b < 0 ) { |
| 37 | DO_512_TIMES( synchronized(lock) { value++; } ) |
| 38 | return 2; |
| 39 | } |
| 40 | return 1; |
| 41 | } |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | Object lock = new Object(); |
| 46 | int value = 0; |
| 47 | } |