【问题标题】:Is possible to have 2 child threads with different classpath in each one?是否可以在每个子线程中有 2 个具有不同类路径的子线程?
【发布时间】:2011-04-01 03:25:01
【问题描述】:

我有一个“核心”应用程序,它是处理任务的适配器。每个任务都由核心在适配器加载中实现,以处理任务。

我的问题是,是否可以在每个适配器中使用不同的类路径来预防适配器之间的类/jar 冲突。

问候,

【问题讨论】:

    标签: java multithreading jvm classpath conflict


    【解决方案1】:

    是的,你可以。使用 Thread 的 setContextClassLoader 方法。

    检查以下链接(有点旧但有用)以更好地理解它:

    http://www.javaworld.com/javaworld/javaqa/2003-06/01-qa-0606-load.html

    【讨论】:

      【解决方案2】:

      确实:

      URLClassLoader cl = new URLClassLoader(urls);
      Thread thread = new MyThread();
      thread.setContextClassLoader(cl);
      thread.start();
      

      【讨论】:

        【解决方案3】:

        使用Thread.currentThread().setContextClassloader() 并使用所需的类路径创建一个新的URLClassLoader

        【讨论】:

          猜你喜欢
          • 2014-07-10
          • 1970-01-01
          • 2013-05-16
          • 1970-01-01
          • 2012-07-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-01-10
          相关资源
          最近更新 更多