Concpets of problem solving
Descartes Scientific Methodology
Java
###
{width=”1.968503937007874in” height=”2.7753958880139984in”}
• 将热点代码保存为机器码
• 将字节码全部保存为机器码
###
{width=”1.968503937007874in” height=”3.7143799212598427in”}
{width=”1.968503937007874in” height=”0.7761986001749781in”}
•
{width=”1.968503937007874in” height=”0.640728346456693in”}
•
{width=”1.968503937007874in” height=”1.1952898075240594in”}
• 构造函数,构造方法和普通方法的区别,只有权限修饰和方法名(和类名相同),虽然没有返回值,但是它没有返回类型,包括void!!
{width=”1.968503937007874in” height=”0.5108737970253718in”}
•
{width=”1.968503937007874in” height=”1.1889632545931759in”}
• 类内部属性全部私有,访问只能通过public 的getter 和setter方法
{width=”1.968503937007874in” height=”0.6310323709536309in”}
• 面向过程思维简单直接,但是缺少复用
•
{width=”1.968503937007874in” height=”0.4417279090113736in”}
• 重要程度如何,在什么工程中必用吗?
• 子主题 1
{width=”1.968503937007874in” height=”1.145559930008749in”}
•
{width=”1.968503937007874in” height=”0.5442541557305337in”}
• 抽象类
•
{width=”1.968503937007874in” height=”0.42678149606299215in”}
• 接口
•
{width=”1.968503937007874in” height=”0.5757152230971129in”}
•
{width=”1.968503937007874in” height=”1.0505358705161856in”}
• 抽象类和接口的异同
•
{width=”1.968503937007874in” height=”1.2721106736657917in”}
•
{width=”1.968503937007874in” height=”0.11665244969378828in”}
•
{width=”1.968503937007874in” height=”0.4182250656167979in”}
• 子类 继承 父类
• 构造方法//todo
• 父类没有无参构造时:
• 定义
{width=”1.968503937007874in” height=”1.4075317147856519in”}
• 子类构造方法在调用时必须先调用父类的,由于父类没有无参构造,必须在子类中显式调用,修改子类构造方法如下即可:
{width=”1.968503937007874in” height=”0.6539435695538057in”}
{width=”1.968503937007874in” height=”0.9593099300087489in”}
• 接口
•
{width=”1.968503937007874in” height=”0.3815354330708661in”}
•
{width=”1.968503937007874in” height=”0.46809383202099736in”}
• 接口和抽象类的关系区别
•
{width=”1.968503937007874in” height=”3.1474234470691163in”}
• java8接口自带适配器了,不用重写接口的所有方法,只需要写需要重写的部分方法
•
{width=”1.968503937007874in” height=”0.6089851268591426in”}
• Generics范型
• Parameterized Type
{width=”1.968503937007874in” height=”1.1657742782152232in”}
•
{width=”1.968503937007874in” height=”0.9198611111111111in”}
•
{width=”1.968503937007874in” height=”0.5106758530183727in”}
•
{width=”1.968503937007874in” height=”0.5291196412948381in”}
•
{width=”1.968503937007874in” height=”0.8735968941382327in”}
• 范型方法
•
{width=”1.968503937007874in” height=”0.9147320647419073in”}
• 可变长参数
•
{width=”1.968503937007874in” height=”1.4883814523184602in”}
•
{width=”1.968503937007874in” height=”1.5331922572178478in”}
•
{width=”1.968503937007874in” height=”0.6615201224846894in”}
• 1.创建Class类调用反射
• 图
• 重要应用:forName就可以读取其他的文本文件在运行动态生成不同对象
{width=”1.968503937007874in” height=”2.504850174978128in”}
• 2.获取一个类的实例对象
•
{width=”1.968503937007874in” height=”1.1963921697287838in”}
• 3.获取对象的基本信息
• 对象名称
• 对象属性
• 对象方法
• 获取构造器信息
•
{width=”1.968503937007874in” height=”0.5021019247594051in”}
• 获取方法
•
{width=”1.968503937007874in” height=”1.187037401574803in”}
•
{width=”1.968503937007874in” height=”1.971790244969379in”}
• 子主题 1
• 调用方法
• . . . 是可变长参数列表
{width=”1.968503937007874in” height=”1.8790266841644794in”}
{width=”1.968503937007874in” height=”0.6228357392825897in”}
• 传输
{width=”1.968503937007874in” height=”3.3874070428696412in”}
• 子主题 1
{width=”1.968503937007874in” height=”0.8705479002624672in”}
{width=”1.968503937007874in” height=”1.1074890638670167in”}
###
{width=”1.968503937007874in” height=”0.8884394138232721in”}
{width=”1.968503937007874in” height=”0.8944028871391076in”}
•
{width=”1.968503937007874in” height=”1.6000678040244969in”}
• round
•
{width=”1.968503937007874in” height=”0.7205468066491688in”}
•
{width=”0.6299212598425197in” height=”0.794248687664042in”}
{width=”1.968503937007874in” height=”0.27864720034995627in”}
•
{width=”0.6299212598425197in” height=”1.5686275153105862in”}
{width=”1.968503937007874in” height=”0.39723206474190725in”}
{width=”1.968503937007874in” height=”1.1401979440069991in”}
• 子主题 1
{width=”1.968503937007874in” height=”0.5288517060367454in”}
• 例题
•
{width=”1.968503937007874in” height=”1.3415244969378828in”}
•
{width=”1.968503937007874in” height=”1.8170800524934383in”}
• 实际使用
{width=”1.968503937007874in” height=”1.3455599300087489in”}
• int i=0 初始化只执行一次
• i<10 每次判断是否进入循环
• i++,循环内部执行完成一次则执行一次i++
• i++以后再次判断i<10是否进入循环
###
{width=”1.968503937007874in” height=”0.2894860017497813in”}
###
{width=”1.968503937007874in” height=”1.8761187664041994in”}
{width=”1.968503937007874in” height=”0.5890015310586176in”}
{width=”1.968503937007874in” height=”1.2480719597550307in”}
###
{width=”1.968503937007874in” height=”1.8730610236220473in”}
###
{width=”1.968503937007874in” height=”1.226923665791776in”}
###
{width=”1.968503937007874in” height=”1.548885608048994in”}
什么是内部类?
{width=”1.968503937007874in” height=”0.24426727909011373in”}
{width=”1.968503937007874in” height=”0.18050306211723535in”}
{width=”1.968503937007874in” height=”1.11708552055993in”}
###
{width=”1.968503937007874in” height=”1.3862707786526685in”}
{width=”1.968503937007874in” height=”0.4295909886264217in”}
{width=”1.968503937007874in” height=”0.2315890201224847in”}
{width=”1.968503937007874in” height=”0.6610870516185476in”}
在程序的运行过程中,对于任何一个类,都能够知道它的所有属性和方法;对于任意一个对象,都能够知道它的任意属性和方法,这种动态获取信息以及动态调用对象方法的功能称为Java语言的反射机制。
book1)的属性和方法以后
{width=”1.968503937007874in” height=”0.8231025809273841in”}
• Introspection与Reflection
•
{width=”1.968503937007874in” height=”0.8332786526684165in”}
• 反射类
•
{width=”1.968503937007874in” height=”0.5730621172353456in”}
• 获取类Class
• 获取类的的方法Method
• 获取构造方法Constructor
• 获取类的属性Field
• 获取class对象的方式的主要三种:
•
{width=”1.968503937007874in” height=”1.4401924759405074in”}
• 根据类名:类名.class
• myBook.classs
• 根据对象:对象.getClass()
• book1.getClass()
• 根据全限定类名:Class.forName(全限定类名)
• Class.forName("demo1.User")
• 获取到class对象以后实例化
• newInstance
•
{width=”1.968503937007874in” height=”0.3515190288713911in”}
• 实例化方式有3种
•
{width=”1.968503937007874in” height=”0.7913254593175854in”}
• 实例
{width=”1.968503937007874in” height=”0.6230413385826772in”}
• 实例2
{width=”1.968503937007874in” height=”1.9567169728783902in”}
{width=”1.968503937007874in” height=”0.5236275153105862in”}
{width=”1.968503937007874in” height=”1.0208486439195101in”}
###
{width=”1.968503937007874in” height=”0.9606572615923009in”}
{width=”1.968503937007874in” height=”0.9146205161854768in”}
• -> 这个指向行为不能改变.B的值可以改变即指针存放的地址初始化之后便不能改变
###
{width=”1.968503937007874in” height=”0.680097331583552in”}
###
{width=”1.968503937007874in” height=”1.8334962817147857in”}
• ==比较的是两个对象的引用值
{width=”1.968503937007874in” height=”1.9533617672790902in”}
• equals对比==== 指针对比equals可以自行重载,如自定义了一个User的equals对比内部数据结构name,age是否相等
{width=”0.23622047244094488in” height=”0.23622047244094488in”}
• 面试常考重点!引用类型的==对比两个对象的指针存的地址值是否相等!
{width=”1.968503937007874in” height=”0.7788429571303587in”}
• Integer(1) 都存在常量池共用一个值
• x地址可能是0x00
• y地址可能是0x04
• 因此引用x,y对象的地址不一样,但是指向是一样的
•
{width=”1.968503937007874in” height=”0.49753390201224845in”}
• 使用原因
•
{width=”1.968503937007874in” height=”0.47374781277340333in”}
•
{width=”1.968503937007874in” height=”1.1868919510061242in”}
• getClass反射获取对象,再获取反射对象的名称
{width=”1.968503937007874in” height=”0.2287062554680665in”}
{width=”1.968503937007874in” height=”0.41236220472440943in”}
###
{width=”1.968503937007874in” height=”3.1486384514435697in”}
{width=”1.968503937007874in” height=”0.8022583114610674in”}
• THM
{width=”1.968503937007874in” height=”1.2573862642169729in”}
• 一般情况下jvm直接线程终止
• io异常
• runtime异常
• 用户自定义异常
可以选择捕获或者抛出异常
图
• try:监控区
{width=”1.968503937007874in” height=”0.7435301837270342in”}
• THM
{width=”1.968503937007874in” height=”1.545945975503062in”}
{width=”1.968503937007874in” height=”0.38575240594925636in”}
{width=”0.23622047244094488in” height=”0.23622047244094488in”}
{width=”1.968503937007874in” height=”1.1789971566054243in”}
{width=”1.968503937007874in” height=”1.4222200349956255in”}
{width=”1.968503937007874in” height=”1.2101968503937008in”}
{width=”0.23622047244094488in” height=”0.23622047244094488in”}
•
{width=”1.968503937007874in” height=”1.039194006999125in”}
• Set
•
{width=”1.968503937007874in” height=”0.3214785651793526in”}
• 红黑树,一种自动平衡的二叉树,高插入和查找性能
• 子主题 1
{width=”1.968503937007874in” height=”0.9451946631671041in”}
•
{width=”1.968503937007874in” height=”1.1188899825021872in”}
•
• 子主题 4
{width=”1.968503937007874in” height=”1.1879647856517934in”}
• 子主题 5
{width=”1.968503937007874in” height=”1.3382174103237094in”}
• 旋转
• 子主题 1
{width=”1.968503937007874in” height=”0.5056091426071742in”}
• Queue也可以用ArrayDeque
{width=”1.968503937007874in” height=”1.4894149168853894in”}
{width=”0.23622047244094488in” height=”0.23622047244094488in”}
• ArrayList
{width=”0.23622047244094488in” height=”0.23622047244094488in”}
• 子主题 1
•
• 如何把id添加进 list数组集合中的Rowpos对象集合中的 指定第一个对象 的id属性中去??
{width=”1.968503937007874in” height=”1.024562554680665in”}
{width=”1.968503937007874in” height=”0.6016590113735784in”}
• extends继承线程类
{width=”1.968503937007874in” height=”0.8899748468941382in”}
• start()启动线程
{width=”1.968503937007874in” height=”0.7135192475940507in”}
• java启动以后是一个java进程,main线程开启了 cat线程,内部名称Thread-0,
• 重要:
{width=”1.968503937007874in” height=”0.1371894138232721in”}
• main线程和cat线程会交替执行
{width=”1.968503937007874in” height=”1.1446073928258969in”}
•
{width=”1.968503937007874in” height=”1.7125371828521434in”}
• 即各 线程独立如果main执行60s,cat执行80smain会自行停止,cat会执行到80s然后退出java进程
• 关联理解后面的守护线程守护线程则不是独立的,是关联型的
•
{width=”1.968503937007874in” height=”1.1914632545931758in”}
•
{width=”1.968503937007874in” height=”0.8273425196850394in”}
• 好处: 不必继承Thread全部方法,简洁,可以实现其他的必要继承
• 用例
•
{width=”1.968503937007874in” height=”0.7158191163604549in”}
•
{width=”1.968503937007874in” height=”0.5988429571303587in”}
• 设计模式:静态代理你没有这个功能/我帮你实现这个功能
• 简单demo
•
{width=”1.968503937007874in” height=”0.772567804024497in”}
•
{width=”1.968503937007874in” height=”0.7287653105861768in”}
•
{width=”1.968503937007874in” height=”0.7064873140857393in”}
{width=”1.968503937007874in” height=”1.1602438757655293in”}
•
{width=”1.968503937007874in” height=”0.9036373578302712in”}
• 不同OS执行多线程的内核实现不同.所以调用底层方法 syscall fork() and exec()
{width=”1.968503937007874in” height=”0.842746062992126in”}
setName()
getName()
• start()
• interrupt()
• 图
•
{width=”1.968503937007874in” height=”0.5197922134733158in”}
•
{width=”1.968503937007874in” height=”0.9519466316710411in”}
• 提前结束休眠继续执行while循环
{width=”1.968503937007874in” height=”0.768908573928259in”}
• sleep()
• 设置线程为TimeWaiting状态
• 多种方法
• 如t.close();
getPriority
setPriority
注意:
•
{width=”1.968503937007874in” height=”0.32808398950131235in”}
{width=”1.968503937007874in” height=”0.44625984251968503in”}
•
{width=”1.968503937007874in” height=”0.22560367454068242in”}
• yied让步视情况执行,如果资源很多则java自动不予让步资源紧张则yield让步实现
• 线程保持Runnable状态
• join以后必须执行完毕插队线程内的所有任务,其他线程全部被设置为线程waiting状态,才执行其他线程
• 例子
• 123ABC45
• 例子
•
{width=”1.968503937007874in” height=”0.4349015748031496in”}
• t2.join() 则让t2插队
{width=”1.968503937007874in” height=”0.8455774278215223in”}
•
{width=”1.968503937007874in” height=”0.5052624671916011in”}
• 用户线程关闭时会自动调用守护线程关闭
• 用户线程:即用户创建的所有线程
•
{width=”1.968503937007874in” height=”0.21950896762904637in”}
{width=”1.968503937007874in” height=”1.3260061242344707in”}
• 调度器: scheduler with in some polocies and algorithm
{width=”1.968503937007874in” height=”1.2947080052493438in”}
• 挂起suspend()建议不要使用/deprecated
• 以及 waiting timeWaiting block terminated
{width=”1.7421259842519685in” height=”1.8208661417322836in”}
•
{width=”1.968503937007874in” height=”1.0213298337707786in”}
• 即内存读写
{width=”1.968503937007874in” height=”0.4586800087489064in”}
• 简单使用
• 图
•
{width=”1.968503937007874in” height=”1.0695538057742782in”}
• 红框二选一使用
{width=”1.968503937007874in” height=”1.6144258530183726in”}
• 锁
• 加锁
• 静态方法时:
• 二选一
{width=”1.968503937007874in” height=”0.7279647856517936in”}
• 加锁位置
{width=”0.23622047244094488in” height=”0.23622047244094488in”}
• todo
• 互斥锁,即锁的全称?
•
{width=”1.968503937007874in” height=”0.3686045494313211in”}
• 释放锁结合运行⑦态图理解
• 图
•
{width=”1.968503937007874in” height=”0.9393383639545057in”}
• 图
•
{width=”1.968503937007874in” height=”0.8622845581802274in”}
• 优化:优先使用同步代码块,锁范围更小,性能可能更高
• 局限
• 图
• 最好直接使用concurrent包
{width=”1.968503937007874in” height=”0.870252624671916in”}
• 注意
• sleep()方法
{width=”1.968503937007874in” height=”0.837179571303587in”}
• 对比
•
{width=”1.968503937007874in” height=”1.5218252405949257in”}
• 解法1
• 对象锁
• 1w张票 9000张无锁剩下1000张票对ticket对象加上synchronized关键词,调用反射方法?运行时动态生成?
• 分段锁?
{width=”1.968503937007874in” height=”0.9681167979002625in”}
• 1
•
{width=”1.968503937007874in” height=”0.3195844269466317in”}
•
{width=”1.968503937007874in” height=”1.0797703412073492in”}
• 2
• 图
•
{width=”1.968503937007874in” height=”1.7125984251968505in”}
• 3
•
{width=”1.968503937007874in” height=”0.8940616797900263in”}
•
{width=”1.9586614173228347in” height=”2.106299212598425in”}
• 图
•
{width=”1.968503937007874in” height=”1.0454735345581803in”}
•
{width=”1.8700787401574803in” height=”0.9251968503937008in”}
{width=”1.968503937007874in” height=”1.2464654418197725in”}
• 方法:
• .read()
• 读取获得到的IO流,读取方式:添加while循环持续读取,文件末尾会读取到-1,read内部存在一个索引index指示器表示读取到字节流的指定位置了
• 单字节
• 指定多字节
• 从指定字节内偏移量读取指定长度字节偏移量尝尝关联前面的index应用在读取图片如105kb图片一次读取100kb,则最后一次读取为read(image,0,len)
•
{width=”1.968503937007874in” height=”1.1244816272965878in”}
• read()返回值为读取到的字节长度 , 如此处设置为1024
{width=”1.968503937007874in” height=”0.7897134733158355in”}
• 方法
• .write()
• 子主题 1
•
{width=”1.968503937007874in” height=”0.4126334208223972in”}
• 图
• 也许4kb 是个不错的buf值?
{width=”1.968503937007874in” height=”0.8754877515310586in”}
•
{width=”1.968503937007874in” height=”0.736172353455818in”}
•
{width=”1.968503937007874in” height=”1.968503937007874in”}
•
{width=”1.968503937007874in” height=”1.3897998687664042in”}
• 子主题 1
• 子主题 1
{width=”1.968503937007874in” height=”0.44819772528433943in”}
• 子主题 2
• 子主题 1
• 子主题 1
{width=”1.968503937007874in” height=”0.7363768591426072in”}
• 简单实现
• 抽象父类
• 子主题 1
• 两个方法可以写为一个,多态自动重载
{width=”1.968503937007874in” height=”0.3911220472440945in”}
• 子类
• 节点流
• 字节读取
• 子主题 1
{width=”1.968503937007874in” height=”0.44797462817147854in”}
• 字符读取
• 子主题 1
{width=”1.968503937007874in” height=”0.4463724846894138in”}
• 包装类
•
{width=”1.968503937007874in” height=”0.7577023184601924in”}
•
{width=”1.968503937007874in” height=”1.128523622047244in”}
• 使用:
• 子主题 1
{width=”1.968503937007874in” height=”0.31116141732283464in”}
• 将多个不同类型的节点流整合为一个包装流
{width=”1.968503937007874in” height=”0.7398709536307961in”}
•
{width=”1.968503937007874in” height=”0.3216819772528434in”}
{width=”1.968503937007874in” height=”0.9459383202099737in”}
• 子主题 1
{width=”1.968503937007874in” height=”0.8737062554680665in”}
{width=”1.968503937007874in” height=”1.4498895450568678in”}
{width=”1.968503937007874in” height=”0.2702701224846894in”}
{width=”1.968503937007874in” height=”1.3099409448818897in”}
• ready 和running 可以归为 一类状态
{width=”1.968503937007874in” height=”0.691729002624672in”}
线程上下文切换越来越频繁,内存占用越来越多
{width=”1.968503937007874in” height=”0.5526421697287839in”}
线程完全不知道合适可以获取到请求的资源,就已经再做后面的操作了. 需要从程序员角度编写异步代码,提前告知OS. 我待会儿要用到xxx资源. Kernel 缓冲区现在没有线程要得资源,告知没有线程反复请求, 造成了大量的Kernel mode 和User mode 的切换编程思路产生了改变
{width=”1.968503937007874in” height=”0.587412510936133in”}
{width=”1.968503937007874in” height=”1.8003827646544182in”}
• 子主题 1
{width=”1.968503937007874in” height=”1.00459864391951in”}
• 应用场景有哪些??
• 子主题 1
{width=”1.968503937007874in” height=”0.12261373578302712in”}
• 子主题 1
{width=”1.968503937007874in” height=”0.9091940069991251in”}
• 子主题 2
{width=”1.968503937007874in” height=”1.339374453193351in”}
• 子主题 3
{width=”1.968503937007874in” height=”1.4869389763779528in”}
• 子主题 4
{width=”1.968503937007874in” height=”0.7599004811898513in”}
{width=”1.968503937007874in” height=”0.3742147856517935in”}
• 即告诉内核要一个IO操作,然后这个进程就去做其他不需要这个IO操作的其他事情,kernel会通知和送达IO数据回来此时可以发现**进程没有waitting状态. 仍然是ready或者running状态**
{width=”1.968503937007874in” height=”0.4734372265966754in”}
• 子主题 2
{width=”1.968503937007874in” height=”1.3188582677165355in”}
• 现在2023 Linux都 6,x 了. 已经有成熟的AIO模型
{width=”1.968503937007874in” height=”1.6583672353455818in”}
{width=”1.968503937007874in” height=”0.3632764654418198in”}
{width=”1.968503937007874in” height=”0.420501968503937in”}
{width=”1.968503937007874in” height=”0.28050524934383203in”}
{width=”1.968503937007874in” height=”0.4205774278215223in”}
{width=”1.968503937007874in” height=”0.8060793963254593in”}
{width=”1.968503937007874in” height=”1.7396084864391952in”}
{width=”1.968503937007874in” height=”1.14290791776028in”}
{width=”1.968503937007874in” height=”2.7807119422572177in”}
{width=”1.968503937007874in” height=”0.8629702537182852in”}
{width=”1.968503937007874in” height=”0.6634186351706036in”}
###
{width=”1.968503937007874in” height=”0.8555424321959755in”}
Descartes Scientific Methodology
Why: Some app is doing unnecessary things or even steal privacy. Monitoring user behaviour: sensitive io files,sensitive upload user privacy file. addin...
ref: https://space.bilibili.com/4848323
Bugs to fix:
What is Software Architect
epub here: 点击下载认知天性
rpc local procedure call to rpc
区块链:
最近读书收获的Take Home Message(better into 4 mem-chunks ) : 非技术类书籍 最喜欢的序:序二 心流人生:一曲冰与火之歌 心熵:心中的信息熵状态: 混乱的时候: 潜意识里,却有多得多的念头在相互冲突,在争夺 你的注意力,在抢夺你大脑的控制权...
Java
业务驱动开发,驱动学习
bytecode 字节码
注册中心的Take Home Message
Computer Network
每日的计划和执行: 搜集所有todo 排序重要和紧急程度 ![[ Eisenhower_matrix.png]] 执行 分配时间片: Schedule time for things that normally distract you. Balance your work and leisur...
The Pragmatic Programmer
Descartes’ scientific methodology, which is a useful way to approach complex problems and seek the truth. However, I understand that sometimes you may encoun...
url: https://youtu.be/DxL2HoqLbyA
“你好同志!请问列宁格勒与斯大林格勒在哪里?我在地图上找不到了” “不会再有了,同志,白匪和资本家重新骑到了我们头上,工人被赶回了工厂,农民被重新戴上了镣铐” “不落的红旗终究也要…” “同志!去东方吧!跨过那第聂伯河!翻过那乌拉尔山!在那遥远的契丹,还有那一丝红星的微光尚存”
年轻永远是最宝贵的财富
Welcome