Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/concurrency problem in deque local #1203

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class RpcInternalContext implements Cloneable {
/**
* The constant DEQUE_LOCAL.
*/
private static final ThreadLocal<Deque<RpcInternalContext>> DEQUE_LOCAL = new TransmittableThreadLocal<Deque<RpcInternalContext>>();
private static final ThreadLocal<Deque<RpcInternalContext>> DEQUE_LOCAL = new ThreadLocal<Deque<RpcInternalContext>>();

/**
* 设置上下文
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,18 @@
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

Expand Down Expand Up @@ -226,6 +232,46 @@ public Object call() throws Exception {
Assert.assertEquals(null, task1.get()[0]);
Assert.assertEquals("TransmittableThreadLocal-value-set-in-parent", task1.get()[1]);
}
}

@Test
public void testConcurrentCall() throws ExecutionException, InterruptedException {
for (int i = 0; i < 20; i++) {
testMultiThreadCall();
}
}

private void testMultiThreadCall() throws ExecutionException, InterruptedException {
RpcInternalContext.getContext();
RpcInternalContext.pushContext();
ExecutorService newThreadPool = new ThreadPoolExecutor(5, 10,
10L, TimeUnit.MILLISECONDS,
new SynchronousQueue<Runnable>());
List<Future<String>> futureList = new ArrayList<>();
for(int i=0; i<20 ;i++){
Future<String> future = newThreadPool.submit(new Callable() {
@Override
public Object call() throws Exception {
try {
RpcInternalContext.getContext();
RpcInternalContext.pushContext();
RpcInternalContext.removeContext();
RpcInternalContext.popContext();
return null;
} catch (NoSuchElementException e) {
Assert.fail();
}
return null;
}
});
futureList.add(future);
}

for (Future future: futureList){
future.get();
}
RpcInvokeContext.removeContext();
RpcInternalContext.popContext();
RpcInternalContext.removeAllContext();
}
}