private static int nextIndex(int i, int len) { return ((i + 1 < len) ? i + 1 : 0); } private static int prevIndex(int i, int len) { return ((i - 1 >= 0) ? i - 1 : len - 1); }
ThreadLocal<Session> threadLocal = new ThreadLocal<Session>(); try { threadLocal.set(new Session(1, "Misout的博客")); // 其它業務邏輯 } finally { threadLocal.remove(); }
//還記得Hibernate的session獲取場景嗎? private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>(); //獲取Session public static Session getCurrentSession(){ Session session = threadLocal.get(); //判斷Session是否爲空,若是爲空,將建立一個session,並設置到本地線程變量中 try { if(session ==null&&!session.isOpen()){ if(sessionFactory==null){ rbuildSessionFactory();// 建立Hibernate的SessionFactory }else{ session = sessionFactory.openSession(); } } threadLocal.set(session); } catch (Exception e) { // TODO: handle exception } return session; }
public class MultiThreadDemo { public static void main(String[] args) throws InterruptedException { private int value = 0; Thread increaseThread = new Thread(new Runnable() { @Override public void run() { try { value = 10; Thread.sleep(10); System.out.println("increase value: " + value); } catch (InterruptedException e) { e.printStackTrace(); } } }); Thread decreaseThread = new Thread(new Runnable() { @Override public void run() { try { value = -10; Thread.sleep(10); System.out.println("decrease value: " + value); } catch (InterruptedException e) { e.printStackTrace(); } } }); increaseThread.start(); decreaseThread.start(); } }
public class SimpleImpl { public static void main(String[] args) throws InterruptedException { private Map<Long, Integer> cacheMap = new HashMap<>(); private int defaultValue = 0 ; Thread increaseThread = new Thread(new Runnable() { @Override public void run() { long id = Thread.currentThread().getId(); cacheMap.put(id, 10); Thread.sleep(10); long id = Thread.currentThread().getId(); if (cacheMap.containsKey(id)) { return cacheMap.get(id); } return defaultValue; } }); Thread decreaseThread = new Thread(new Runnable() { @Override public void run() { long id = Thread.currentThread().getId(); cacheMap.put(id, -10); Thread.sleep(10); long id = Thread.currentThread().getId(); if (cacheMap.containsKey(id)) { return cacheMap.get(id); } return defaultValue; } }); increaseThread.start(); decreaseThread.start(); } }
public class SimpleImpl2 { public static class CommonThread extends Thread { Map<Integer, Integer> cacheMap = new HashMap<>(); } public static class Number { public void increase() throws InterruptedException { Integer id = this.hashCode(); Map<Integer, Integer> cacheMap = (CommonThread) Thread.currentThread().cacheMap; cacheMap.put(id, 10); Thread.sleep(10); return cacheMap.get(id); } public void decrease() throws InterruptedException { Integer id = this.hashCode(); Map<Integer, Integer> cacheMap = (CommonThread) Thread.currentThread().cacheMap; cacheMap.put(id, -10); Thread.sleep(10); return cacheMap.get(id); } } public static void main(String[] args) throws InterruptedException { final Number number = new Number(); Thread increaseThread = new CommonThread() { @Override public void run() { number.increase(); } }; Thread decreaseThread = new CommonThread() { @Override public void run() { number.decrease(); } }; increaseThread.start(); decreaseThread.start(); } }
在上面的實現中,當線程消亡以後,線程中 cacheMap 也會被回收,它當中存放的副本變量(value)也會被所有回收,而且 cacheMap 是線程私有的,不會出現多個線程同時訪問一個 cacheMap 的狀況。在 Java 中,ThreadLocal 類的實現就是採用的這種思想,注意只是思想,實際的實現和上面的並不同。session