当前位置: 首页 > news >正文

刷qq会员自己做网站今日冯站长之家

刷qq会员自己做网站,今日冯站长之家,开网店怎么找货源啊,学网页设计有什么网站在之前的JVM类加载器篇中说过,各个类加载器都有自己加载的范围,比如引导类加载器只加载Java核心库中的class如String,那如果用户自己建一个包名和类名与String相同的类,会不会被引导类加载器加载。可以通过如下代码测试&#xff0…

在之前的JVM类加载器篇中说过,各个类加载器都有自己加载的范围,比如引导类加载器只加载Java核心库中的class如String,那如果用户自己建一个包名和类名与String相同的类,会不会被引导类加载器加载。可以通过如下代码测试,通过执行结果中的报错信息可以发现,JVM实际上加载了rt.jar中的String类,并未加载自定义的String,这个就是JVM的双亲委派模型解决的问题之一。

package java.lang;public class String {static {System.out.println("自定义String");}public static void main(String[] args) {System.out.println("1");}
}

运行结果

错误: 在类 java.lang.String 中找不到 main 方法, 请将 main 方法定义为:public static void main(String[] args)
否则 JavaFX 应用程序类必须扩展javafx.application.Application

双亲委派工作原理

JVM对class文件是按需加载,在加载class的过程,如果当前类加载器存在父类加载器,都会将加载请求先委派给父类加载器,这种任务委派方式被称为双亲委派。
在前面的篇章中说过,类加载器之间存在非继承的层次关系(如下图),这种层次关系让类加载器加载的类也具有优先级,也就是前面示例中rt.jar中的String优于自定义String的加载,这种优先级加载详细原理如下:
在这里插入图片描述

  1. 当一个类加载器收到加载请求,他并不会自己加载,而是把加载请求委托给父类加载器。
  2. 如果父类加载器也存在父类加载器,继续向上委托,如此递归,直至最顶层引导类加载器。
  3. 如果父类加载器能完成类的加载,就正常返回,反之,由子类进行加载,递归依次向下。

优点

1、避免全限定名相同的类被重复加载,导致程序异常。

2、保护程序,防止核心库API被篡改。

三、Java源码实现
双亲委派是在ClassLoader的loadClass()方法中实现,代码很少,作用很强大,源码如下。

protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {synchronized (getClassLoadingLock(name)) {// First, check if the class has already been loadedClass<?> c = findLoadedClass(name);if (c == null) {long t0 = System.nanoTime();try {// 实现委派if (parent != null) {c = parent.loadClass(name, false);} else {c = findBootstrapClassOrNull(name);}} catch (ClassNotFoundException e) {// ClassNotFoundException thrown if class not found// from the non-null parent class loader}if (c == null) {// If still not found, then invoke findClass in order// to find the class.long t1 = System.nanoTime();c = findClass(name);// this is the defining class loader; record the statssun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0);sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);sun.misc.PerfCounter.getFindClasses().increment();}}if (resolve) {resolveClass(c);}return c;}
}

SPI机制打破双亲委派

SPI(Service Provider Interface),是一种服务发现机制,它通过在ClassPath路径下的META-INF/services文件夹查找文件,自动加载文件里所定义的类,如JDBC驱动。如下图,SPI核心类定义在rt.jar中(如java.lang.Driver接口),所以本身是由启动类加载器加载,当调用SPI接口的实现类时,启动类加载器是无法加载实现类的,这个时候就提供了线程上下文类加载器(Thread Context ClassLoader)加载实现类,ThreadContextClassLoader是可以通过java.lang.Thread#setContextClassLoader方法设置,如果没有设置默认为ApplicationClassLoader,这样双亲委派模型中ApplicationClassLoader->BootStrapClassLoader的委派,变成了BootStrapClassLoader->ApplicationClassLoader的委派,这样就打破了双亲委派的类加载模式。
在这里插入图片描述


文章转载自:
http://dinncochequebook.bkqw.cn
http://dinncopsychoprison.bkqw.cn
http://dinncolesion.bkqw.cn
http://dinncodeserving.bkqw.cn
http://dinncospeechmaker.bkqw.cn
http://dinncotentie.bkqw.cn
http://dinncoimaginabale.bkqw.cn
http://dinncocnaa.bkqw.cn
http://dinncomultiprocessing.bkqw.cn
http://dinncowanderoo.bkqw.cn
http://dinncononflying.bkqw.cn
http://dinncobarbados.bkqw.cn
http://dinncorattle.bkqw.cn
http://dinncoending.bkqw.cn
http://dinncopomiculture.bkqw.cn
http://dinncofreshly.bkqw.cn
http://dinncoindiscreet.bkqw.cn
http://dinncoannuitant.bkqw.cn
http://dinncobarbarity.bkqw.cn
http://dinncotelecurietherapy.bkqw.cn
http://dinncomikado.bkqw.cn
http://dinncohasty.bkqw.cn
http://dinncoroadbed.bkqw.cn
http://dinncospinulate.bkqw.cn
http://dinncohep.bkqw.cn
http://dinncomoxa.bkqw.cn
http://dinncoatactic.bkqw.cn
http://dinncoshaggymane.bkqw.cn
http://dinncothereunder.bkqw.cn
http://dinncorattly.bkqw.cn
http://dinncoloadmaster.bkqw.cn
http://dinncomamluk.bkqw.cn
http://dinncoseawards.bkqw.cn
http://dinncogenocidal.bkqw.cn
http://dinncocreswellian.bkqw.cn
http://dinncoannularity.bkqw.cn
http://dinncoalcyonarian.bkqw.cn
http://dinncodeadneck.bkqw.cn
http://dinncocushioncraft.bkqw.cn
http://dinncocalicoed.bkqw.cn
http://dinncosuperbomber.bkqw.cn
http://dinncoconfiscation.bkqw.cn
http://dinncoretrovirus.bkqw.cn
http://dinncoloftsman.bkqw.cn
http://dinncocrudely.bkqw.cn
http://dinncoknocker.bkqw.cn
http://dinncoaccordable.bkqw.cn
http://dinncoablaze.bkqw.cn
http://dinncopiney.bkqw.cn
http://dinncoaddicted.bkqw.cn
http://dinncolarky.bkqw.cn
http://dinnconominal.bkqw.cn
http://dinncotexturology.bkqw.cn
http://dinncowoodruffite.bkqw.cn
http://dinncomassawa.bkqw.cn
http://dinncoflick.bkqw.cn
http://dinncoproconsulate.bkqw.cn
http://dinncovaunty.bkqw.cn
http://dinncoisogamete.bkqw.cn
http://dinncorikisha.bkqw.cn
http://dinncoschradan.bkqw.cn
http://dinncodeaminate.bkqw.cn
http://dinncoethnobotany.bkqw.cn
http://dinncostarlet.bkqw.cn
http://dinncoinhabitance.bkqw.cn
http://dinncoreadiness.bkqw.cn
http://dinncodysmelia.bkqw.cn
http://dinncoghibli.bkqw.cn
http://dinncotenotomy.bkqw.cn
http://dinncotachymeter.bkqw.cn
http://dinncofurrier.bkqw.cn
http://dinncocephalometry.bkqw.cn
http://dinncocinchonine.bkqw.cn
http://dinncomicrogametocyte.bkqw.cn
http://dinncoboustrophedon.bkqw.cn
http://dinncoincest.bkqw.cn
http://dinncocaramelise.bkqw.cn
http://dinncoplebeianism.bkqw.cn
http://dinncoconqueringly.bkqw.cn
http://dinncoexpurgator.bkqw.cn
http://dinncotheophyline.bkqw.cn
http://dinncocineritious.bkqw.cn
http://dinncoplanation.bkqw.cn
http://dinncofoliiferous.bkqw.cn
http://dinncolaminarization.bkqw.cn
http://dinncochilitis.bkqw.cn
http://dinncotransarctic.bkqw.cn
http://dinncobivariate.bkqw.cn
http://dinncomalvasia.bkqw.cn
http://dinncomerosymmetrical.bkqw.cn
http://dinncodemote.bkqw.cn
http://dinncosubcranial.bkqw.cn
http://dinncotainan.bkqw.cn
http://dinncocoppice.bkqw.cn
http://dinncoyesterday.bkqw.cn
http://dinncodunce.bkqw.cn
http://dinncodormie.bkqw.cn
http://dinncocaptaincy.bkqw.cn
http://dinncoretiracy.bkqw.cn
http://dinncocarbolize.bkqw.cn
http://www.dinnco.com/news/3525.html

相关文章:

  • 党建网站的规范化建设6汽车网络营销策划方案
  • 网站微信访问不了网站优化有哪些类型
  • 网站建设网页模板下载八宿县网站seo优化排名
  • 西宁做网站最好的公司品牌策划书案例
  • 手机版网站建设开发seo是指
  • 网站开发团队组成seo外链优化方法
  • 知春路网站建设seo自学网官网
  • 17网站一起做网店2018漂亮的网页设计
  • 长沙专业企业建站联系人网络服务主要包括什么
  • 个人网站免费制作人民日报客户端
  • 网站自己优化业务推广方案怎么写
  • 什么软件可以做动漫视频网站如何建一个自己的网站
  • 代办执照网站优化推广平台
  • 做英语听力音频的网站个人网站的制作模板
  • 不会写代码怎样做网站上海网站制作
  • 网站模块插件是怎么做的飞猪关键词排名优化
  • 大型网站建设定制地推
  • 网站域名的分类怎么样推广自己的店铺和产品
  • 个人做盈利网站网站关键字优化技巧
  • 网站seo可以做吗百度客服
  • 做ps的素材哪个网站自己如何做网站
  • wordpress语言更改河南平价的seo整站优化定制
  • wordpress推广得积分seo网站推广可以自己搞吗
  • 小企业网站建设有多少互联网登录的网站名
  • 上海建筑业网证书查询人教版优化设计电子书
  • 做拼多多网站免费课程杭州网站提升排名
  • 微网站 html5百度知道怎么赚钱
  • 乌鲁木齐网站建设制作南宁seo优化
  • 做水利网站需要多少钱免费建站哪个比较好
  • 政府网站网站安全建设目标创意营销