Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/imageio/spi/ServiceRegistrySyncTest.java
41149 views
1
/*
2
* Copyright (c) 2016, 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
/**
25
* @test
26
* @bug 8022640
27
* @summary Test verifies whether ServiceProvider API's of
28
* ServiceRegistry are safe for concurrent access.
29
* @run main ServiceRegistrySyncTest
30
*/
31
32
import java.io.File;
33
import java.io.IOException;
34
import java.util.ArrayList;
35
import java.util.Locale;
36
import javax.imageio.spi.ImageInputStreamSpi;
37
import javax.imageio.spi.ServiceRegistry;
38
import javax.imageio.stream.ImageInputStream;
39
40
public class ServiceRegistrySyncTest {
41
public static void main(String[] args) throws InterruptedException {
42
43
final ArrayList<Class<?>> services = new ArrayList<Class<?>>();
44
services.add(ImageInputStreamSpi.class);
45
46
final ServiceRegistry reg = new ServiceRegistry(services.iterator());
47
48
//create new thread for Registerer and Consumer Class
49
Thread registerer = new Thread(new Registerer(reg));
50
Thread consumer = new Thread(new Consumer(reg));
51
52
//run both registerer and consumer thread parallely
53
registerer.start();
54
consumer.start();
55
}
56
57
static class Consumer implements Runnable {
58
private final ServiceRegistry reg;
59
boolean go = true;
60
int duration;
61
long start, end;
62
63
public Consumer(ServiceRegistry r) {
64
reg = r;
65
//set 5000ms duration to run the test
66
duration = 5000;
67
}
68
69
@Override
70
public void run() {
71
start = System.currentTimeMillis();
72
end = start + duration;
73
while (start < end) {
74
//access the ServiceProvider API
75
reg.getServiceProviders(ImageInputStreamSpi.class, true);
76
start = System.currentTimeMillis();
77
}
78
}
79
}
80
81
static class Registerer implements Runnable {
82
private final ServiceRegistry reg;
83
boolean go = true;
84
int duration;
85
long start, end;
86
87
public Registerer(ServiceRegistry r) {
88
reg = r;
89
//set 5000ms duration to run the test
90
duration = 5000;
91
}
92
93
@Override
94
public void run() {
95
final int N = 20;
96
97
MyService[] services = new MyService[N];
98
for (int i = 0; i < N; i++) {
99
services[i] = new MyService();
100
}
101
start = System.currentTimeMillis();
102
end = start + duration;
103
while (start < end) {
104
//access the ServiceProvider API's
105
for (int i = 0; i < N; i++) {
106
reg.registerServiceProvider(services[i]);
107
}
108
for (int i = 0; i < N; i++) {
109
reg.deregisterServiceProvider(services[i]);
110
}
111
start = System.currentTimeMillis();
112
}
113
}
114
}
115
}
116
117
class MyService extends ImageInputStreamSpi {
118
public MyService() {
119
}
120
121
@Override
122
public String getDescription(Locale locale) {
123
return null;
124
}
125
126
@Override
127
public ImageInputStream createInputStreamInstance
128
(Object input, boolean useCache, File cacheDir) throws IOException {
129
return null;
130
}
131
}
132
133