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
31import java.io.IOException;
32import java.nio.file.Files;
33import java.nio.file.Path;
34import java.util.ArrayList;
35
36public 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    }
71
72    public void start() throws IOException {
73        for (int i = 0; i < numThreads; i++) {
74            final Path p = createTmpFile();
75            Runnable r = createRunnable(p);
76            Thread thread = new Thread(r, "thread-" + i);
77            thread.start();
78            threads.add(thread);
79        }
80    }
81
82    public void join() {
83        for (Thread thread : threads) {
84            try {
85                thread.join();
86            } catch (InterruptedException e) {
87                // ignore it and proceed to the next one
88            }
89        }
90    }
91
92    public static void main(String[] args) throws Exception {
93        ParallelProbes probes =
94            new ParallelProbes(args.length < 1 ? 1 : Integer.parseInt(args[0]));
95        probes.start();
96        probes.join();
97    }
98}
99