AutoreleaseBatcher extends OperationBatcher { public static AutoreleaseBatcher forThread(int batchSize) { if (threadLocal.get() == null) { threadLocal.set(new AutoreleaseBatcher(batchSize)); } return threadLocal.get(); } AutoreleaseBatcher(int batchSize); static AutoreleaseBatcher forThread(int batchSize);  }
@Test public void threadLocal() { AutoreleaseBatcher.forThread(1); ID idNSObject = Foundation.cfRetain(autoreleasedObject()); assertRetainCount(2, idNSObject); AutoreleaseBatcher.forThread(1).operate(); assertRetainCount(1, idNSObject); }
@Test public void threadLocal2Threads() throws InterruptedException { AutoreleaseBatcher.forThread(1); ID idNSObject = Foundation.cfRetain(autoreleasedObject()); assertRetainCount(2, idNSObject); Thread thread = new Thread() { public void run() { try { AutoreleaseBatcher.forThread(1); ID idNSObject = Foundation.cfRetain(autoreleasedObject()); assertRetainCount(2, idNSObject); AutoreleaseBatcher.forThread(1).operate(); assertRetainCount(1, idNSObject); } catch (Throwable t) { thrown = t; } }}; thread.run(); thread.join(); AutoreleaseBatcher.forThread(1).operate(); assertRetainCount(1, idNSObject); assertNull(thrown); }