question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Why PoolThreadLocalCache class initialValue method use synchronized ???

See original GitHub issue
final class PoolThreadLocalCache extends FastThreadLocal<PoolThreadCache> {
        private final boolean useCacheForAllThreads;

        PoolThreadLocalCache(boolean useCacheForAllThreads) {
            this.useCacheForAllThreads = useCacheForAllThreads;
        }

        @Override
        protected synchronized PoolThreadCache initialValue() {
            final PoolArena<byte[]> heapArena = leastUsedArena(heapArenas);
            final PoolArena<ByteBuffer> directArena = leastUsedArena(directArenas);

            final Thread current = Thread.currentThread();
            if (useCacheForAllThreads || current instanceof FastThreadLocalThread) {
                final PoolThreadCache cache = new PoolThreadCache(
                        heapArena, directArena, tinyCacheSize, smallCacheSize, normalCacheSize,
                        DEFAULT_MAX_CACHED_BUFFER_CAPACITY, DEFAULT_CACHE_TRIM_INTERVAL);

                if (DEFAULT_CACHE_TRIM_INTERVAL_MILLIS > 0) {
                    final EventExecutor executor = ThreadExecutorMap.currentExecutor();
                    if (executor != null) {
                        executor.scheduleAtFixedRate(trimTask, DEFAULT_CACHE_TRIM_INTERVAL_MILLIS,
                                DEFAULT_CACHE_TRIM_INTERVAL_MILLIS, TimeUnit.MILLISECONDS);
                    }
                }
                return cache;
            }
            // No caching so just use 0 as sizes.
            return new PoolThreadCache(heapArena, directArena, 0, 0, 0, 0, 0);
        }

I think that’s unnecessary, initialValue method use synchronized ???

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jiangxinlingducommented, Jul 12, 2019

@normanmaurerhttps://github.com/netty/netty/issues/9284 About FastThreadLocal Cache line padding I haven’t figured it out yet, hope you can explain. Thank you

0reactions
normanmaurercommented, Jul 12, 2019
Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Do operations on ThreadLocal have to be synchronized?
ThreadLocal is itself a threadsafety mechanism, and it manages its threadsafety better than you could get by tacking on synchronized anyway.
Read more >
io.netty.buffer.PooledByteBufAllocator$PoolThreadLocalCache java ...
@Override protected synchronized PoolThreadCache initialValue() { final PoolArena<byte[]> heapArena = leastUsedArena(heapArenas); final ...
Read more >
Allocate from Thread's Cache - Happy Coding
The initalValue method for PoolThreadLocalCache. @Override protected synchronized PoolThreadCache initialValue() { final PoolArena<byte[]> ...
Read more >
Synchronization - Learning Java, 4th Edition [Book] - O'Reilly
Fortunately, Java makes the process of synchronizing access to resources ... All static synchronized methods in a class use the same class object...
Read more >
PooledByteBufAllocator
getInstance(PooledByteBufAllocator.class); private static final int ... spots as * allocation and de-allocation needs to be synchronized on the PoolArena.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found