File tree Expand file tree Collapse file tree 2 files changed +42
-11
lines changed
src/main/java/code/spi/extension Expand file tree Collapse file tree 2 files changed +42
-11
lines changed Original file line number Diff line number Diff line change @@ -164,21 +164,26 @@ private void init(){
164164 }
165165 }
166166
167- public synchronized S find (String name ){
167+ public S find (String name ){
168168 //if exist in cache
169169 if (providers .containsKey (name )){
170170 return providers .get (name );
171171 }
172172
173- //lookup
174- if (acc == null ) {
175- return findService (name );
176- } else {
177- PrivilegedAction <S > action = new PrivilegedAction <S >() {
178- @ Override
179- public S run () { return findService (name ); }
180- };
181- return AccessController .doPrivileged (action , acc );
173+ synchronized (this ){
174+ if (providers .containsKey (name )){
175+ return providers .get (name );
176+ }
177+ //lookup
178+ if (acc == null ) {
179+ return findService (name );
180+ } else {
181+ PrivilegedAction <S > action = new PrivilegedAction <S >() {
182+ @ Override
183+ public S run () { return findService (name ); }
184+ };
185+ return AccessController .doPrivileged (action , acc );
186+ }
182187 }
183188 }
184189
Original file line number Diff line number Diff line change 33import code .serialize .ISerializer ;
44import code .serialize .ProtostuffSerializer ;
55import code .spi .Words ;
6-
76import org .junit .Test ;
87import org .springframework .util .Assert ;
98
9+ import java .util .concurrent .CountDownLatch ;
10+ import java .util .concurrent .ExecutorService ;
11+ import java .util .concurrent .Executors ;
12+ import java .util .concurrent .TimeUnit ;
13+
1014/**
1115 * 〈一句话功能简述〉<p>
1216 * 〈功能详细描述〉
@@ -33,4 +37,26 @@ public void testFind(){
3337 System .out .println (serializer .deserialize (bytes , Words .class ));
3438 }
3539
40+ @ Test
41+ public void test () throws InterruptedException {
42+ int threads = 20 ;
43+ CountDownLatch latch = new CountDownLatch (threads );
44+ ExecutorService executorService = Executors .newFixedThreadPool (threads );
45+ for (int i =0 ; i <threads ; i ++){
46+ executorService .execute (() -> {
47+ latch .countDown ();
48+ try {
49+ latch .await ();
50+ } catch (InterruptedException e ) {
51+ e .printStackTrace ();
52+ }
53+ System .out .println (System .nanoTime ());
54+ ISerializer serializer = extensionLoader .find ("json" );
55+ byte [] bytes = serializer .serialize (Words .HELLO );
56+ System .out .println (serializer .deserialize (bytes , Words .class ));
57+ });
58+ }
59+ executorService .shutdown ();
60+ executorService .awaitTermination (5 , TimeUnit .SECONDS );
61+ }
3662}
You can’t perform that action at this time.
0 commit comments