然而并不能啊。在设置 classLoader 前加个 Thead.sleep() 就知道不能了。你的结论就是错的。本质上是线程 1 先运行线程 2 才运行的。
```java
public class Main {
public static void main(String[] args) {
Thread mainThread = Thread.currentThread();
new Thread(new Runnable() {
@
Override public void run() {
try {
ClassLoader cl = new ClassLoader() {
@
Override public String getName() {
return "MyCloassLoader";
}
};
mainThread.setContextClassLoader(cl);
System.out.println("在" + Thread.currentThread().getName() + "设置了主线程的自定义 classLoader " + cl);
} catch (Exception e) {
e.printStackTrace();
}
}
}, "线程 1").start();
new Thread(new Runnable() {
@
Override public void run() {
try {
for (int i = 0; i < 100; i++) {
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName() + "上下文 设置前的 classloader" + Thread.currentThread().getContextClassLoader());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}, "线程 2").start();
}
}
```