Path: blob/master/test/jdk/javax/imageio/spi/ServiceRegistrySyncTest.java
41149 views
/*1* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 802264026* @summary Test verifies whether ServiceProvider API's of27* ServiceRegistry are safe for concurrent access.28* @run main ServiceRegistrySyncTest29*/3031import java.io.File;32import java.io.IOException;33import java.util.ArrayList;34import java.util.Locale;35import javax.imageio.spi.ImageInputStreamSpi;36import javax.imageio.spi.ServiceRegistry;37import javax.imageio.stream.ImageInputStream;3839public class ServiceRegistrySyncTest {40public static void main(String[] args) throws InterruptedException {4142final ArrayList<Class<?>> services = new ArrayList<Class<?>>();43services.add(ImageInputStreamSpi.class);4445final ServiceRegistry reg = new ServiceRegistry(services.iterator());4647//create new thread for Registerer and Consumer Class48Thread registerer = new Thread(new Registerer(reg));49Thread consumer = new Thread(new Consumer(reg));5051//run both registerer and consumer thread parallely52registerer.start();53consumer.start();54}5556static class Consumer implements Runnable {57private final ServiceRegistry reg;58boolean go = true;59int duration;60long start, end;6162public Consumer(ServiceRegistry r) {63reg = r;64//set 5000ms duration to run the test65duration = 5000;66}6768@Override69public void run() {70start = System.currentTimeMillis();71end = start + duration;72while (start < end) {73//access the ServiceProvider API74reg.getServiceProviders(ImageInputStreamSpi.class, true);75start = System.currentTimeMillis();76}77}78}7980static class Registerer implements Runnable {81private final ServiceRegistry reg;82boolean go = true;83int duration;84long start, end;8586public Registerer(ServiceRegistry r) {87reg = r;88//set 5000ms duration to run the test89duration = 5000;90}9192@Override93public void run() {94final int N = 20;9596MyService[] services = new MyService[N];97for (int i = 0; i < N; i++) {98services[i] = new MyService();99}100start = System.currentTimeMillis();101end = start + duration;102while (start < end) {103//access the ServiceProvider API's104for (int i = 0; i < N; i++) {105reg.registerServiceProvider(services[i]);106}107for (int i = 0; i < N; i++) {108reg.deregisterServiceProvider(services[i]);109}110start = System.currentTimeMillis();111}112}113}114}115116class MyService extends ImageInputStreamSpi {117public MyService() {118}119120@Override121public String getDescription(Locale locale) {122return null;123}124125@Override126public ImageInputStream createInputStreamInstance127(Object input, boolean useCache, File cacheDir) throws IOException {128return null;129}130}131132133