Skip to content

Instantly share code, notes, and snippets.

@sp3c73r2038
Created February 2, 2015 17:00
Show Gist options
  • Save sp3c73r2038/c0a9982e81626d157eb5 to your computer and use it in GitHub Desktop.
Save sp3c73r2038/c0a9982e81626d157eb5 to your computer and use it in GitHub Desktop.
Java ThreadLocal demo
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