Bael 822 thread local (#1625)
* code for thread local article * userNameSecret * better to string * fix typo
This commit is contained in:
parent
2ac22b3ffc
commit
a5879bf8b7
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.threadlocal;
|
||||
|
||||
|
||||
public class Context {
|
||||
private final String userName;
|
||||
|
||||
public Context(String userName) {
|
||||
this.userName = userName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Context{" +
|
||||
"userNameSecret='" + userName + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.baeldung.threadlocal;
|
||||
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class SharedMapWithUserContext implements Runnable {
|
||||
public final static Map<Integer, Context> userContextPerUserId = new ConcurrentHashMap<>();
|
||||
private final Integer userId;
|
||||
private UserRepository userRepository = new UserRepository();
|
||||
|
||||
public SharedMapWithUserContext(Integer userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
String userName = userRepository.getUserNameForUserId(userId);
|
||||
userContextPerUserId.put(userId, new Context(userName));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package com.baeldung.threadlocal;
|
||||
|
||||
|
||||
public class ThreadLocalWithUserContext implements Runnable {
|
||||
private static final ThreadLocal<Context> userContext = new ThreadLocal<>();
|
||||
private final Integer userId;
|
||||
private UserRepository userRepository = new UserRepository();
|
||||
|
||||
public ThreadLocalWithUserContext(Integer userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
String userName = userRepository.getUserNameForUserId(userId);
|
||||
userContext.set(new Context(userName));
|
||||
System.out.println("thread context for given userId: " + userId + " is: " + userContext.get());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package com.baeldung.threadlocal;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
|
||||
public class UserRepository {
|
||||
public String getUserNameForUserId(Integer userId) {
|
||||
return UUID.randomUUID().toString();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
package com.baeldung.threadlocal;
|
||||
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class ThreadLocalTest {
|
||||
@Test
|
||||
public void givenThreadThatStoresContextInAMap_whenStartThread_thenShouldSetContextForBothUsers() throws ExecutionException, InterruptedException {
|
||||
//when
|
||||
SharedMapWithUserContext firstUser = new SharedMapWithUserContext(1);
|
||||
SharedMapWithUserContext secondUser = new SharedMapWithUserContext(2);
|
||||
new Thread(firstUser).start();
|
||||
new Thread(secondUser).start();
|
||||
|
||||
Thread.sleep(3000);
|
||||
//then
|
||||
assertEquals(SharedMapWithUserContext.userContextPerUserId.size(), 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenThreadThatStoresContextInThreadLocal_whenStartThread_thenShouldStoreContextInThreadLocal() throws ExecutionException, InterruptedException {
|
||||
//when
|
||||
ThreadLocalWithUserContext firstUser = new ThreadLocalWithUserContext(1);
|
||||
ThreadLocalWithUserContext secondUser = new ThreadLocalWithUserContext(2);
|
||||
new Thread(firstUser).start();
|
||||
new Thread(secondUser).start();
|
||||
|
||||
Thread.sleep(3000);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue