2019独角兽企业重金招聘Python工程师标准>>>

What is different between Thread.currentThread().getContextClassLoader() and Class.getClassLoader()?

From API document, the Thread.currentThread().getContextClassLoader() returns the context ClassLoader for this Thread. The context ClassLoader is provided by the creator of the thread for use by code running in this thread when loading classes and resources. The default is the ClassLoader context of the parent Thread. The context ClassLoader of the primordial thread is typically set to the class loader used to load the application. The context ClassLoader can be set when a thread is created, and allows the creator of the thread to provide the appropriate class loader to code running in the thread when loading classes and resources. For example, JNDI and JAXP used thread's ClassLoader. You had better to use thread's ClassLoader in your own code when your code need to deployed on J2EE container.

The Class.getClassLoader() returns the class loader for the class. Some implementations may use null to represent the bootstrap class loader. This method will return null in such implementations if this class was loaded by the bootstrap class loader. For example, Class.getResource() and Class.forName() will use the class loader of trhe current caller's class.

The Understanding Class.forName() by Ted Neward is excellent paper on ClassLoader.

DriverManager.getConnection is example:http://daizuan.iteye.com/blog/1138683
System.out.println(DriverManager.class.getClassLoader()); 
控制台输出null
表明DriverManager的classLoader是相当NB的bootstrap ClassLoader
而我们自己写的类一般都是由systemClassloader加载的。
如果在DriverManager直接class.forName("com.mysql.jdbc.ReplicationDriver"),就表示要让bootstrap ClassLoader来加载jdk/lib目录下的com.mysql.jdbc.ReplicationDriver,可能么??明显不在那里,而是在classpath目录下。
所以要这么来class.forName("com.mysql.jdbc.ReplicationDriver",true, callerClassLoader)

另外如果getCallerClassLoader()返回了空,则会获取当前线程上下文的classLoader .

转载于:https://my.oschina.net/gavinjin/blog/800382

Thread.currentThread().getContextClassLoader()和Class.getClassLoader()区别相关推荐

  1. Thread.currentThread().getContextClassLoader() 和 Class.getClassLoader()区别

    查了一些资料也不是太明白两个的区别,但是前者是最安全的用法 打个简单的比方,你一个WEB程序,发布到Tomcat里面运行. 首先是执行Tomcat org.apache.catalina.startu ...

  2. Thread.currentThread().getContextClassLoader().getResourceAsStream()读取配置文件

    Java中使用的路径,分为两种:绝对路径和相对路径.具体而言,又分为四种: 一.URI形式的绝对资源路径 如:file:/D:/java/eclipse/workspace/j/bin/a URL是U ...

  3. Thread.interrupted()与Thread.currentThread().isInterrupted()区别

    背景 今天在研究ThreadPoolExecutor源码时,在runWorker(Worker w)的方法中有一个判断条件分别用到了Thread.interrupted()和Thread.curren ...

  4. android getid,Process.myTid()和Thread.currentThread().getId()区别

    首先,两个方法都是返回线程ID,但结果是不同的,简单的说 android.os.Process.myTid()是系统级的ID Thread.currentThread().getId()是Java级的 ...

  5. Thread.currentThread()与this的区别

    在自定义线程类时,如果线程类是继承java.lang.Thread的话,那么线程类就可以使用this关键字去调用继承自父类Thread的方法,this就是当前的对象. 另一方面,Thread.curr ...

  6. [面试] Thread 中 start() 和 run() 的区别都不知道,还怎么混?

    引子 最近面试了不少 Java 工程师,有一些心得体会想给大家分享,比如,上次就有一个小哥被我 "送走" 了,我尽量复原一下当时的面试情景,对话大致如下: 我:我们知道,JDK 中 ...

  7. 关于 Thread.currentThread()

    关于 Thread.currentThread() currentThread()  到底是什么? 其实currentThread() 只是Thread 的一个静态方法.返回的正是执行当前代码指令的线 ...

  8. Thread.currentThread()方法、进程、线程、多线程相关总结(二)

    Thread.currentThread()方法 Thread.currentThread()可以获取当前线程的引用,一般都是在没有线程对象又需要获得线程信息时通过Thread.currentThre ...

  9. Thread.currentThread()、isAlive()、Thread.sleep()的使用

    1.Thread.currentThread()方法: 返回当前正在运行的线程 一个简单的例子: MyThread_7.Java类的构造函数是被main线程调用的,而run()方法是被名为Thread ...

最新文章

  1. TemplateBuilder
  2. 分分钟甩Word几条街,Python编辑公式竟可以如此简单,你都知道吗?
  3. leetcode - 674. 最长连续递增序列
  4. maven常见问题归纳
  5. C++---之Arraylist
  6. 透过“简书钻”来探究简书的商业模式转变
  7. CentOS下安装配置MySQL8.0的步骤详解
  8. c语言数字字符一起读,如何同时输入字符串和数字
  9. 电信aep平台是什么意思_亚马逊电商平台是什么意思?亚马逊注册技巧
  10. React Router 4 简介及其背后的路由哲学
  11. mac80端口问题无法使用
  12. 连续9年惠及10万贫困家庭 金科“情暖万家”春节送温暖再出发
  13. 印象笔记在Windows上创建手写笔记
  14. 【Kotlin 初学者】Java和Kotlin互操作
  15. Java基础 DAY03
  16. python中判断一个数是否为素数_【转载】Python脚本判断一个数是否为素数的几种方法...
  17. 《联盟-互联网时代的人才变革》读后感
  18. android 模拟器实现发短信
  19. 【数字化】分享-广东省企业首席数据官建设指南
  20. 使用ANTLR进行语法分析

热门文章

  1. 好程序员分享如何看待CSS中BEM的命名方式?
  2. Docker容器系列教程(三):jenkins环境搭建与插件安装
  3. 爱奇艺全国高校算法大赛初赛A
  4. skynet源码阅读4--定时器实现
  5. centos下搭建dhcp服务器
  6. rundeck 内存溢出
  7. python 对一个函数执行速度控制的演示
  8. [原创]使用 Google Chart 在线服务实现软件版本发布时间线图
  9. 学习笔记---将Asp.Net网站发布到IIS的四种方法及注意事项
  10. OpenCV学习笔记总结