View Javadoc

1   package org.apache.helix.filestore;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.Date;
25  import java.util.List;
26  import java.util.concurrent.Callable;
27  import java.util.concurrent.ExecutorService;
28  import java.util.concurrent.Executors;
29  import java.util.concurrent.Future;
30  import java.util.concurrent.atomic.AtomicReference;
31  
32  public class Test
33  {
34    public static void main(String[] args) throws InterruptedException
35    {
36      while(true){
37      ExecutorService service = Executors.newFixedThreadPool(400);
38      List<Callable<Object>> list = new ArrayList<Callable<Object>>();
39      for (int i = 0; i < 256; i++)
40      {
41        MyRunnable runnable = new MyRunnable();
42        list.add(runnable);
43        new Thread(runnable).start();
44        // service.submit(runnable);
45      }
46      
47  //    List<Future<Object>> invokeAll = service.invokeAll(list);
48  //    service.shutdownNow();
49      System.out.println("RUN -------------");
50      Thread.sleep(5000);
51      }
52    }
53  }
54  
55  class MyRunnable implements Callable<Object>,Runnable
56  {
57  
58    // AtomicReference<Object> ref = new AtomicReference<Object>(new Object());
59    class Ref
60    {
61      Object obj;
62  
63      public Ref(Object obj)
64      {
65        this.obj = obj;
66      }
67  
68      void set(Object obj)
69      {
70        this.obj = obj;
71      }
72  
73      Object get()
74      {
75        return obj;
76      }
77    }
78  
79    Ref ref = new Ref(new Object());
80    Object lock = new Object();
81  
82    //@Override
83    public Object call1() throws Exception
84    {
85      long start = System.currentTimeMillis();
86      System.out.println(start);
87      synchronized (lock)
88      {
89  
90        if (ref.get() != null)
91        {
92          try
93          {
94            int sum = 0;
95            List<Float> array = new ArrayList<Float>();
96            for (int i = 0; i < 5000; i++)
97            {
98              array.add((float) Math.random());
99            }
100           Collections.sort(array);
101         } catch (Exception e)
102         {
103           e.printStackTrace();
104         }
105         ref.set(null);
106       }
107     }
108     long end = System.currentTimeMillis();
109     System.out.println("time:" + (end - start));
110     return new Object();
111 
112   }
113 
114   @Override
115   public Object call() throws Exception
116   {
117     long start = System.currentTimeMillis();
118     System.out.println(start);
119     try
120     {
121 //      int sum = 0;
122 //      List<Float> array = new ArrayList<Float>();
123 //      for (int i = 0; i < 5000; i++)
124 //      {
125 //        array.add((float) Math.random());
126 //      }
127       //Collections.sort(array);
128     } catch (Exception e)
129     {
130       e.printStackTrace();
131     }
132     ref.set(null);
133     long end = System.currentTimeMillis();
134     System.out.println("time:" + (end - start));
135     return new Object();
136 
137   }
138 
139   @Override
140   public void run()
141   {
142     long start = System.currentTimeMillis();
143     System.out.println(start);
144     try
145     {
146       int sum = 0;
147       List<Float> array = new ArrayList<Float>();
148       for (int i = 0; i < 5000; i++)
149       {
150         array.add((float) Math.random());
151       }
152       Collections.sort(array);
153     } catch (Exception e)
154     {
155       e.printStackTrace();
156     }
157     ref.set(null);
158     long end = System.currentTimeMillis();
159     System.out.println("time:" + (end - start));    
160   }
161 }
162