1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.io.IOException;
  25 import java.nio.file.Files;
  26 import java.nio.file.Path;
  27 import java.util.ArrayList;
  28 
  29 /* @test
  30  * @summary Test probing content type simultaneously from multiple threads.
  31  * @requires (os.family == "linux") | (os.family == "solaris")
  32  * @run main/othervm ParallelProbes 10
  33  */
  34 public class ParallelProbes {
  35 
  36     private static final int REPEATS = 1000;
  37 
  38     private int numThreads = 0;
  39     private ArrayList<Thread> threads;
  40 
  41     public ParallelProbes(int numThreads) {
  42         System.out.println("Using <" + numThreads + "> threads.");
  43         this.numThreads = numThreads;
  44         this.threads = new ArrayList<Thread>(numThreads);
  45     }
  46 
  47     private Path createTmpFile() throws IOException {
  48         final Path p = Files.createTempFile("prefix", ".json");
  49         Files.write(p, "{\"test\"}".getBytes());
  50         System.out.println("Write test file <" + p + ">");
  51         return p;
  52     }
  53 
  54     private Runnable createRunnable(final Path p) {
  55         Runnable r = new Runnable() {
  56             public void run() {
  57                 for (int i = 0; i < REPEATS; i++) {
  58                     try {
  59                         System.out.println(Thread.currentThread().getName()
  60                             + " -> " + Files.probeContentType(p));
  61                     } catch (IOException ioException) {
  62                         ioException.printStackTrace();
  63                     }
  64                 }
  65             }
  66         };
  67         return r;
  68     }
  69 
  70     public void start() throws IOException {
  71         for (int i = 0; i < numThreads; i++) {
  72             final Path p = createTmpFile();
  73             Runnable r = createRunnable(p);
  74             Thread thread = new Thread(r, "thread-" + i);
  75             thread.start();
  76             threads.add(thread);
  77         }
  78     }
  79 
  80     public void join() {
  81         for (Thread thread : threads) {
  82             try {
  83                 thread.join();
  84             } catch (InterruptedException e) {
  85                 // ignore it and proceed to the next one
  86             }
  87         }
  88     }
  89 
  90     public static void main(String[] args) throws Exception {
  91         ParallelProbes probes =
  92             new ParallelProbes(args.length < 1 ? 1 : Integer.parseInt(args[0]));
  93         probes.start();
  94         probes.join();
  95     }
  96 }