• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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 import java.lang.reflect.Method;
18 import java.lang.reflect.Type;
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.concurrent.Callable;
22 import java.util.concurrent.Executors;
23 import java.util.concurrent.ExecutorService;
24 import java.util.concurrent.Future;
25 import java.util.concurrent.TimeUnit;
26 import java.util.concurrent.CancellationException;
27 import java.util.concurrent.TimeoutException;
28 
29 public class Main {
30   private static class HashCodeQuery implements Callable<Integer> {
HashCodeQuery(Object obj)31     public HashCodeQuery(Object obj) {
32       m_obj = obj;
33     }
34 
call()35     public Integer call() {
36       Integer result;
37       try {
38         Class<?> c = Class.forName("Test");
39         Method m = c.getMethod("synchronizedHashCode", Object.class);
40         result = (Integer) m.invoke(null, m_obj);
41       } catch (Exception e) {
42         System.out.println("Hash code query exception");
43         e.printStackTrace(System.out);
44         result = -1;
45       }
46       return result;
47     }
48 
49     private Object m_obj;
50     private int m_index;
51   }
52 
main(String args[])53   public static void main(String args[]) throws Exception {
54     Object obj = new Object();
55     int numThreads = 10;
56 
57     ExecutorService pool = Executors.newFixedThreadPool(numThreads);
58 
59     List<HashCodeQuery> queries = new ArrayList<HashCodeQuery>(numThreads);
60     for (int i = 0; i < numThreads; ++i) {
61       queries.add(new HashCodeQuery(obj));
62     }
63 
64     try {
65       List<Future<Integer>> results = pool.invokeAll(queries);
66 
67       int hash = obj.hashCode();
68       for (int i = 0; i < numThreads; ++i) {
69         int result = results.get(i).get();
70         if (hash != result) {
71           throw new Error("Query #" + i + " wrong. Expected " + hash + ", got " + result);
72         }
73       }
74       pool.shutdown();
75     } catch (CancellationException ex) {
76       System.out.println("Job timeout");
77       System.exit(1);
78     }
79   }
80 }
81