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