Created
February 2, 2015 17:00
-
-
Save sp3c73r2038/c0a9982e81626d157eb5 to your computer and use it in GitHub Desktop.
Java ThreadLocal demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.momoka; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
/** | |
* Hello world! | |
* | |
*/ | |
public class App implements Runnable { | |
private static final Logger LOGGER = LoggerFactory.getLogger(App.class); | |
private Long operatorId; | |
public App(Long operatorId) { | |
this.operatorId = operatorId; | |
} | |
public static void main(String[] args) { | |
App app1 = new App(1L); | |
App app2 = new App(2L); | |
Thread t1 = new Thread(app1); | |
Thread t2 = new Thread(app2); | |
t1.start(); | |
t2.start(); | |
} | |
@Override | |
public void run() { | |
before(); | |
after(); | |
} | |
private void before() { | |
RequestThreadLocal.operatorId.set(this.operatorId); | |
} | |
private void after() { | |
Long _ = RequestThreadLocal.operatorId.get(); | |
LOGGER.debug("{}", _); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment