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

alibaba / jetcache / #422

27 Apr 2025 06:35AM UTC coverage: 88.775% (-0.01%) from 88.785%
#422

push

web-flow
fix: use JetCacheExecutor.class 's class loader as JetCacheExecutor thread's context class loader (#975)

3 of 4 new or added lines in 1 file covered. (75.0%)

2 existing lines in 1 file now uncovered.

4753 of 5354 relevant lines covered (88.77%)

0.89 hits per line

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

85.37
/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 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.
NEW
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
                    return t;
1✔
77
                };
78
                heavyIOExecutor = new ScheduledThreadPoolExecutor(
1✔
79
                        10, tf, new ThreadPoolExecutor.DiscardPolicy());
80
            }
81
        }finally {
82
            reentrantLock.unlock();
1✔
83
        }
84
        return heavyIOExecutor;
1✔
85
    }
86

87
    public static void setDefaultExecutor(ScheduledExecutorService executor) {
88
        JetCacheExecutor.defaultExecutor = executor;
×
89
    }
×
90

91
    public static void setHeavyIOExecutor(ScheduledExecutorService heavyIOExecutor) {
92
        JetCacheExecutor.heavyIOExecutor = heavyIOExecutor;
×
93
    }
×
94
}
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