Skip to content

Commit a5879bf

Browse files
tomekl007yetanotherallisonf
authored andcommitted
Bael 822 thread local (eugenp#1625)
* code for thread local article * userNameSecret * better to string * fix typo
1 parent 2ac22b3 commit a5879bf

5 files changed

Lines changed: 103 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.threadlocal;
2+
3+
4+
public class Context {
5+
private final String userName;
6+
7+
public Context(String userName) {
8+
this.userName = userName;
9+
}
10+
11+
@Override
12+
public String toString() {
13+
return "Context{" +
14+
"userNameSecret='" + userName + '\'' +
15+
'}';
16+
}
17+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.threadlocal;
2+
3+
4+
import java.util.Map;
5+
import java.util.concurrent.ConcurrentHashMap;
6+
7+
public class SharedMapWithUserContext implements Runnable {
8+
public final static Map<Integer, Context> userContextPerUserId = new ConcurrentHashMap<>();
9+
private final Integer userId;
10+
private UserRepository userRepository = new UserRepository();
11+
12+
public SharedMapWithUserContext(Integer userId) {
13+
this.userId = userId;
14+
}
15+
16+
@Override
17+
public void run() {
18+
String userName = userRepository.getUserNameForUserId(userId);
19+
userContextPerUserId.put(userId, new Context(userName));
20+
}
21+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.threadlocal;
2+
3+
4+
public class ThreadLocalWithUserContext implements Runnable {
5+
private static final ThreadLocal<Context> userContext = new ThreadLocal<>();
6+
private final Integer userId;
7+
private UserRepository userRepository = new UserRepository();
8+
9+
public ThreadLocalWithUserContext(Integer userId) {
10+
this.userId = userId;
11+
}
12+
13+
14+
@Override
15+
public void run() {
16+
String userName = userRepository.getUserNameForUserId(userId);
17+
userContext.set(new Context(userName));
18+
System.out.println("thread context for given userId: " + userId + " is: " + userContext.get());
19+
}
20+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.threadlocal;
2+
3+
import java.util.UUID;
4+
5+
6+
public class UserRepository {
7+
public String getUserNameForUserId(Integer userId) {
8+
return UUID.randomUUID().toString();
9+
}
10+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.threadlocal;
2+
3+
4+
import org.junit.Test;
5+
6+
import java.util.concurrent.ExecutionException;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class ThreadLocalTest {
11+
@Test
12+
public void givenThreadThatStoresContextInAMap_whenStartThread_thenShouldSetContextForBothUsers() throws ExecutionException, InterruptedException {
13+
//when
14+
SharedMapWithUserContext firstUser = new SharedMapWithUserContext(1);
15+
SharedMapWithUserContext secondUser = new SharedMapWithUserContext(2);
16+
new Thread(firstUser).start();
17+
new Thread(secondUser).start();
18+
19+
Thread.sleep(3000);
20+
//then
21+
assertEquals(SharedMapWithUserContext.userContextPerUserId.size(), 2);
22+
}
23+
24+
@Test
25+
public void givenThreadThatStoresContextInThreadLocal_whenStartThread_thenShouldStoreContextInThreadLocal() throws ExecutionException, InterruptedException {
26+
//when
27+
ThreadLocalWithUserContext firstUser = new ThreadLocalWithUserContext(1);
28+
ThreadLocalWithUserContext secondUser = new ThreadLocalWithUserContext(2);
29+
new Thread(firstUser).start();
30+
new Thread(secondUser).start();
31+
32+
Thread.sleep(3000);
33+
}
34+
35+
}

0 commit comments

Comments
 (0)