< prev index next >

test/java/nio/file/Files/probeContentType/ParallelProbes.java

Print this page


   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 /* @test
  25  * @summary Test probing content type simultaneously from multiple threads.
  26  * @requires (os.family == "linux") | (os.family == "solaris")
  27  * @build ParallelProbes SimpleFileTypeDetector
  28  * @run main/othervm ParallelProbes 10
  29  */
  30 
  31 import java.io.IOException;
  32 import java.nio.file.Files;
  33 import java.nio.file.Path;

  34 import java.util.ArrayList;
  35 
  36 public class ParallelProbes {
  37 
  38     private static final int REPEATS = 1000;
  39 
  40     private int numThreads = 0;
  41     private ArrayList<Thread> threads;
  42 
  43     public ParallelProbes(int numThreads) {
  44         System.out.println("Using <" + numThreads + "> threads.");
  45         this.numThreads = numThreads;
  46         this.threads = new ArrayList<Thread>(numThreads);
  47     }
  48 
  49     private Path createTmpFile() throws IOException {
  50         final Path p = Files.createTempFile("prefix", ".json");

  51         Files.write(p, "{\"test\"}".getBytes());
  52         System.out.println("Write test file <" + p + ">");
  53         return p;
  54     }
  55 
  56     private Runnable createRunnable(final Path p) {
  57         Runnable r = new Runnable() {
  58             public void run() {
  59                 for (int i = 0; i < REPEATS; i++) {
  60                     try {
  61                         System.out.println(Thread.currentThread().getName()
  62                             + " -> " + Files.probeContentType(p));
  63                     } catch (IOException ioException) {
  64                         ioException.printStackTrace();
  65                     }
  66                 }
  67             }
  68         };
  69         return r;
  70     }


   1 /*
   2  * Copyright (c) 2015, 2017, 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 /* @test
  25  * @summary Test probing content type simultaneously from multiple threads.
  26  * @requires (os.family == "linux") | (os.family == "solaris")
  27  * @build ParallelProbes SimpleFileTypeDetector
  28  * @run main/othervm ParallelProbes 10
  29  */
  30 
  31 import java.io.IOException;
  32 import java.nio.file.Files;
  33 import java.nio.file.Path;
  34 import java.nio.file.Paths;
  35 import java.util.ArrayList;
  36 
  37 public class ParallelProbes {
  38 
  39     private static final int REPEATS = 1000;
  40 
  41     private int numThreads = 0;
  42     private ArrayList<Thread> threads;
  43 
  44     public ParallelProbes(int numThreads) {
  45         System.out.println("Using <" + numThreads + "> threads.");
  46         this.numThreads = numThreads;
  47         this.threads = new ArrayList<Thread>(numThreads);
  48     }
  49 
  50     private Path createTmpFile() throws IOException {
  51         Path dir = Paths.get(System.getProperty("test.dir", "."));
  52         final Path p = Files.createTempFile(dir, "prefix", ".json");
  53         Files.write(p, "{\"test\"}".getBytes());
  54         System.out.println("Write test file <" + p + ">");
  55         return p;
  56     }
  57 
  58     private Runnable createRunnable(final Path p) {
  59         Runnable r = new Runnable() {
  60             public void run() {
  61                 for (int i = 0; i < REPEATS; i++) {
  62                     try {
  63                         System.out.println(Thread.currentThread().getName()
  64                             + " -> " + Files.probeContentType(p));
  65                     } catch (IOException ioException) {
  66                         ioException.printStackTrace();
  67                     }
  68                 }
  69             }
  70         };
  71         return r;
  72     }


< prev index next >