java.lang.Thread.setContextClassLoader()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(8.9k)|赞(0)|评价(0)|浏览(169)

本文整理了Java中java.lang.Thread.setContextClassLoader()方法的一些代码示例,展示了Thread.setContextClassLoader()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Thread.setContextClassLoader()方法的具体详情如下:
包路径:java.lang.Thread
类名称:Thread
方法名:setContextClassLoader

Thread.setContextClassLoader介绍

[英]Sets the context ClassLoader for this Thread. The context ClassLoader can be set when a thread is created, and allows the creator of the thread to provide the appropriate class loader, through getContextClassLoader, to code running in the thread when loading classes and resources.

If a security manager is present, its SecurityManager#checkPermission(java.security.Permission)method is invoked with a RuntimePermission("setContextClassLoader") permission to see if setting the context ClassLoader is permitted.
[中]设置此线程的上下文类加载器。可以在创建线程时设置上下文类加载器,并允许线程的创建者通过getContextClassLoader提供适当的类加载器,以便在加载类和资源时在线程中运行代码。
如果存在安全管理器,将使用RuntimePermission(“setContextClassLoader”)权限调用其SecurityManager#checkPermission(java.security.Permission)方法,以查看是否允许设置上下文类加载器。

代码示例

代码示例来源:origin: netty/netty

@Override
  public Void run() {
    watcherThread.setContextClassLoader(null);
    return null;
  }
});

代码示例来源:origin: netty/netty

@Override
  public Void run() {
    t.setContextClassLoader(null);
    return null;
  }
});

代码示例来源:origin: netty/netty

@Override
  public Void run() {
    cleanupThread.setContextClassLoader(null);
    return null;
  }
});

代码示例来源:origin: spring-projects/spring-framework

/**
 * Reset the original thread context ClassLoader if necessary.
 * @param original the original thread context ClassLoader,
 * or {@code null} if not overridden (and hence nothing to reset)
 */
protected void resetThreadContextClassLoader(@Nullable ClassLoader original) {
  if (original != null) {
    Thread.currentThread().setContextClassLoader(original);
  }
}

代码示例来源:origin: jenkinsci/jenkins

@Override public Thread newThread(Runnable r) {
    Thread t = delegate.newThread(r);
    t.setContextClassLoader(ClassLoaderSanityThreadFactory.class.getClassLoader());
    return t;
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Switches the {@link Thread#getContextClassLoader() context ClassLoader} for the
 * managed resources {@link ClassLoader} before allowing the invocation to occur.
 * @see javax.management.modelmbean.ModelMBean#setAttributes
 */
@Override
public AttributeList setAttributes(AttributeList attributes) {
  ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
  try {
    Thread.currentThread().setContextClassLoader(this.managedResourceClassLoader);
    return super.setAttributes(attributes);
  }
  finally {
    Thread.currentThread().setContextClassLoader(currentClassLoader);
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Switches the {@link Thread#getContextClassLoader() context ClassLoader} for the
 * managed resources {@link ClassLoader} before allowing the invocation to occur.
 * @see javax.management.modelmbean.ModelMBean#getAttributes
 */
@Override
public AttributeList getAttributes(String[] attrNames) {
  ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
  try {
    Thread.currentThread().setContextClassLoader(this.managedResourceClassLoader);
    return super.getAttributes(attrNames);
  }
  finally {
    Thread.currentThread().setContextClassLoader(currentClassLoader);
  }
}

代码示例来源:origin: apache/incubator-dubbo

@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
  ClassLoader ocl = Thread.currentThread().getContextClassLoader();
  Thread.currentThread().setContextClassLoader(invoker.getInterface().getClassLoader());
  try {
    return invoker.invoke(invocation);
  } finally {
    Thread.currentThread().setContextClassLoader(ocl);
  }
}

代码示例来源:origin: apache/incubator-dubbo

@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
  ClassLoader ocl = Thread.currentThread().getContextClassLoader();
  Thread.currentThread().setContextClassLoader(invoker.getInterface().getClassLoader());
  try {
    return invoker.invoke(invocation);
  } finally {
    Thread.currentThread().setContextClassLoader(ocl);
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Switches the {@link Thread#getContextClassLoader() context ClassLoader} for the
 * managed resources {@link ClassLoader} before allowing the invocation to occur.
 * @see javax.management.modelmbean.ModelMBean#invoke
 */
@Override
public Object invoke(String opName, Object[] opArgs, String[] sig)
    throws MBeanException, ReflectionException {
  ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
  try {
    Thread.currentThread().setContextClassLoader(this.managedResourceClassLoader);
    return super.invoke(opName, opArgs, sig);
  }
  finally {
    Thread.currentThread().setContextClassLoader(currentClassLoader);
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Switches the {@link Thread#getContextClassLoader() context ClassLoader} for the
 * managed resources {@link ClassLoader} before allowing the invocation to occur.
 * @see javax.management.modelmbean.ModelMBean#getAttribute
 */
@Override
public Object getAttribute(String attrName)
    throws AttributeNotFoundException, MBeanException, ReflectionException {
  ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
  try {
    Thread.currentThread().setContextClassLoader(this.managedResourceClassLoader);
    return super.getAttribute(attrName);
  }
  finally {
    Thread.currentThread().setContextClassLoader(currentClassLoader);
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Switches the {@link Thread#getContextClassLoader() context ClassLoader} for the
 * managed resources {@link ClassLoader} before allowing the invocation to occur.
 * @see javax.management.modelmbean.ModelMBean#setAttribute
 */
@Override
public void setAttribute(Attribute attribute)
    throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException {
  ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
  try {
    Thread.currentThread().setContextClassLoader(this.managedResourceClassLoader);
    super.setAttribute(attribute);
  }
  finally {
    Thread.currentThread().setContextClassLoader(currentClassLoader);
  }
}

代码示例来源:origin: jenkinsci/jenkins

@Override
  public void run() {
    Thread t = Thread.currentThread();
    String name = t.getName();
    ClassLoader cl = t.getContextClassLoader();
    try {
      r.run();
    } finally {
      t.setName(name);
      t.setContextClassLoader(cl);
    }
  }
};

代码示例来源:origin: jenkinsci/jenkins

@Override
  public V call() throws Exception {
    Thread t = Thread.currentThread();
    String name = t.getName();
    ClassLoader cl = t.getContextClassLoader();
    try {
      return r.call();
    } finally {
      t.setName(name);
      t.setContextClassLoader(cl);
    }
  }
};

代码示例来源:origin: spring-projects/spring-framework

/**
 * Override the thread context ClassLoader with the environment's bean ClassLoader
 * if necessary, i.e. if the bean ClassLoader is not equivalent to the thread
 * context ClassLoader already.
 * @param classLoaderToUse the actual ClassLoader to use for the thread context
 * @return the original thread context ClassLoader, or {@code null} if not overridden
 */
@Nullable
public static ClassLoader overrideThreadContextClassLoader(@Nullable ClassLoader classLoaderToUse) {
  Thread currentThread = Thread.currentThread();
  ClassLoader threadContextClassLoader = currentThread.getContextClassLoader();
  if (classLoaderToUse != null && !classLoaderToUse.equals(threadContextClassLoader)) {
    currentThread.setContextClassLoader(classLoaderToUse);
    return threadContextClassLoader;
  }
  else {
    return null;
  }
}

代码示例来源:origin: google/guava

@Override
protected void tearDown() throws Exception {
 classReloader.close();
 Thread.currentThread().setContextClassLoader(oldClassLoader);
 System.clearProperty("guava.concurrent.generate_cancellation_cause");
}

代码示例来源:origin: google/guava

public void testGetResource_contextClassLoaderNull() {
 ClassLoader oldContextLoader = Thread.currentThread().getContextClassLoader();
 try {
  Thread.currentThread().setContextClassLoader(null);
  assertNotNull(Resources.getResource("com/google/common/io/testdata/i18n.txt"));
  try {
   Resources.getResource("no such resource");
   fail("Should get IllegalArgumentException");
  } catch (IllegalArgumentException expected) {
  }
 } finally {
  Thread.currentThread().setContextClassLoader(oldContextLoader);
 }
}

代码示例来源:origin: eclipse-vertx/vert.x

static Vertx createVertxLoadingMetricsFromMetaInf(VertxOptions options, String factoryFqn) {
 ClassLoader oldCL = Thread.currentThread().getContextClassLoader();
 ClassLoader cl = createMetricsFromMetaInfLoader(factoryFqn);
 Thread.currentThread().setContextClassLoader(cl);
 try {
  return Vertx.vertx(options);
 } finally {
  Thread.currentThread().setContextClassLoader(oldCL);
 }
}

代码示例来源:origin: google/guava

@Override
public void runTest() throws Exception {
 // First ensure that our classloaders are initializing the correct helper versions
 checkHelperVersion(getClass().getClassLoader(), "SafeAtomicHelper");
 checkHelperVersion(NO_ATOMIC_FIELD_UPDATER, "SynchronizedAtomicHelper");
 // Run the corresponding FuturesTest test method in a new classloader that disallows
 // certain core jdk classes.
 ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
 Thread.currentThread().setContextClassLoader(NO_ATOMIC_FIELD_UPDATER);
 try {
  runTestMethod(NO_ATOMIC_FIELD_UPDATER);
  // TODO(lukes): assert that the logs are full of errors
 } finally {
  Thread.currentThread().setContextClassLoader(oldClassLoader);
 }
}

代码示例来源:origin: eclipse-vertx/vert.x

@Override
 public void after() throws Exception {
  if (prevCL != null) {
   Thread.currentThread().setContextClassLoader(prevCL);
  }
  super.after();
 }
}

相关文章