Bael 822 thread local (#1625)

* code for thread local article

* userNameSecret

* better to string

* fix typo
This commit is contained in:
Tomasz Lelek 2017-04-14 04:16:53 +02:00 committed by yetanotherallisonf
parent 2ac22b3ffc
commit a5879bf8b7
5 changed files with 103 additions and 0 deletions

View File

@ -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 + '\'' +
'}';
}
}

View File

@ -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));
}
}

View File

@ -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());
}
}

View File

@ -0,0 +1,10 @@
package com.baeldung.threadlocal;
import java.util.UUID;
public class UserRepository {
public String getUserNameForUserId(Integer userId) {
return UUID.randomUUID().toString();
}
}

View File

@ -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);
}
}