• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

alibaba / jetcache / #423

27 Apr 2025 06:38AM UTC coverage: 88.82% (+0.05%) from 88.775%
#423

push

areyouok
fix: use classloader of jetcache as heavyIOExecutor thread context classloader

4 of 5 new or added lines in 1 file covered. (80.0%)

4759 of 5358 relevant lines covered (88.82%)

0.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.44
/jetcache-core/src/main/java/com/alicp/jetcache/support/JetCacheExecutor.java
1
package com.alicp.jetcache.support;
2

3
import java.util.concurrent.ScheduledExecutorService;
4
import java.util.concurrent.ScheduledThreadPoolExecutor;
5
import java.util.concurrent.ThreadFactory;
6
import java.util.concurrent.ThreadPoolExecutor;
7
import java.util.concurrent.atomic.AtomicInteger;
8
import java.util.concurrent.locks.ReentrantLock;
9

10
/**
11
 * Created on 2017/5/3.
12
 *
13
 * @author huangli
14
 */
15
public class JetCacheExecutor {
×
16
    protected volatile static ScheduledExecutorService defaultExecutor;
17
    protected volatile static ScheduledExecutorService heavyIOExecutor;
18
    private static final ReentrantLock reentrantLock = new ReentrantLock();
1✔
19

20
    private static final AtomicInteger threadCount = new AtomicInteger(0);
1✔
21

22
    static {
23
        Runtime.getRuntime().addShutdownHook(new Thread() {
1✔
24
            @Override
25
            public void run() {
26
                if (defaultExecutor != null) {
1✔
27
                    defaultExecutor.shutdownNow();
1✔
28
                }
29
                if (heavyIOExecutor != null) {
1✔
30
                    heavyIOExecutor.shutdownNow();
1✔
31
                }
32
            }
1✔
33
        });
34
    }
1✔
35

36
    public static ScheduledExecutorService defaultExecutor() {
37
        if (defaultExecutor != null) {
1✔
38
            return defaultExecutor;
1✔
39
        }
40
        reentrantLock.lock();
1✔
41
        try{
42
            if (defaultExecutor == null) {
1✔
43
                ThreadFactory tf = r -> {
1✔
44
                    Thread t = new Thread(r, "JetCacheDefaultExecutor");
1✔
45
                    t.setDaemon(true);
1✔
46

47
                    ClassLoader classLoader = JetCacheExecutor.class.getClassLoader();
1✔
48
                    if (classLoader == null) {
1✔
49
                        // This class was loaded by the Bootstrap ClassLoader,
50
                        // so let's tie the thread's context ClassLoader to the System ClassLoader instead.
51
                        classLoader = ClassLoader.getSystemClassLoader();
×
52
                    }
53
                    t.setContextClassLoader(classLoader);
1✔
54

55
                    return t;
1✔
56
                };
57
                int coreSize = Math.min(4, Runtime.getRuntime().availableProcessors());
1✔
58
                defaultExecutor = new ScheduledThreadPoolExecutor(coreSize, tf);
1✔
59
            }
60
        }finally {
61
            reentrantLock.unlock();
1✔
62
        }
63
        return defaultExecutor;
1✔
64
    }
65

66
    public static ScheduledExecutorService heavyIOExecutor() {
67
        if (heavyIOExecutor != null) {
1✔
68
            return heavyIOExecutor;
1✔
69
        }
70
        reentrantLock.lock();
1✔
71
        try {
72
            if (heavyIOExecutor == null) {
1✔
73
                ThreadFactory tf = r -> {
1✔
74
                    Thread t = new Thread(r, "JetCacheHeavyIOExecutor" + threadCount.getAndIncrement());
1✔
75
                    t.setDaemon(true);
1✔
76
                    ClassLoader classLoader = JetCacheExecutor.class.getClassLoader();
1✔
77
                    if (classLoader == null) {
1✔
78
                        // This class was loaded by the Bootstrap ClassLoader,
79
                        // so let's tie the thread's context ClassLoader to the System ClassLoader instead.
NEW
80
                        classLoader = ClassLoader.getSystemClassLoader();
×
81
                    }
82
                    t.setContextClassLoader(classLoader);
1✔
83
                    return t;
1✔
84
                };
85
                heavyIOExecutor = new ScheduledThreadPoolExecutor(
1✔
86
                        10, tf, new ThreadPoolExecutor.DiscardPolicy());
87
            }
88
        }finally {
89
            reentrantLock.unlock();
1✔
90
        }
91
        return heavyIOExecutor;
1✔
92
    }
93

94
    public static void setDefaultExecutor(ScheduledExecutorService executor) {
95
        JetCacheExecutor.defaultExecutor = executor;
×
96
    }
×
97

98
    public static void setHeavyIOExecutor(ScheduledExecutorService heavyIOExecutor) {
99
        JetCacheExecutor.heavyIOExecutor = heavyIOExecutor;
×
100
    }
×
101
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc