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

淘宝做网站退款seo广告优化多少钱

淘宝做网站退款,seo广告优化多少钱,wordpress管理员修改密码,美食网站建设策划书范文目录 Day 3:多线程(1)1. 线程1.1 引入线程的原因1.2 线程的定义1.3 为何线程更轻量1.4 问题 2. 多线程代码2.1 继承Thread重写run2.2 通过实现Runnable接口创建线程2.3 针对2.1的变形使用匿名内部类2.4 针对Runnable创建匿名内部类2.5 使用la…

目录

  • Day 3:多线程(1)
    • 1. 线程
      • 1.1 引入线程的原因
      • 1.2 线程的定义
      • 1.3 为何线程更轻量
      • 1.4 问题
    • 2. 多线程代码
      • 2.1 继承Thread重写run
      • 2.2 通过实现Runnable接口创建线程
      • 2.3 针对2.1的变形使用匿名内部类
      • 2.4 针对Runnable创建匿名内部类
      • 2.5 使用lambda表达式

Day 3:多线程(1)

C++会对进程有更进一步介绍,例如:如何通过编写代码,来进行进程的控制(多进程编程),但是Java并不太关注这些

  • JVM没有提供上述多进程编程的api
  • Java生态中也不太鼓励使用多进程编程

JVM也不是完全没有,也提供了非常粗糙的多进程操作的api,但是控制过程不如C++通过系统原生api更精细

1. 线程

1.1 引入线程的原因

当前的CPU都是多核心CPU,需要通过一些特定的编程技巧,把要完成的任务,拆解成多个部分,并且分别让他们在不同的CPU核心上运行,也就是**“并发编程”**

  • 通过多进程编程的模式,其实就可以起到“并发编程”的效果,因为进程可以被调度到不同的CPU上运行,此时就可以把多个CPU核心都给很好的利用起来,虽然,多进程编程可以解决上述问题,也带来了新的麻烦
  • 在服务器开发中,并发编程的需求场景非常常见,所以一个服务器要能够同时给多个客户端提供服务,如果同一时间,来了很多客户端,服务器如果只能利用一个CPU核心工作,速度就会比较慢
  • 一种典型的做法:每个客户端连上服务器了,服务器都创建一个进程,给客户端提供服务,这个客户端断开了,服务器再把进程给释放掉,如果这个服务器,频繁的有客户端来来去去,服务器就需要频繁创建/销毁进程

所以引入线程,来解决上述进程“太重量”的问题

1.2 线程的定义

线程(thread),也称为“轻量级进程”,创建和销毁的开销更小,线程可以理解成“进程的一部分”,一个进程中可以包含一个线程或者多个线程,描述进程,使用PCB这样的结构体,事实上,更严格地说,一个PCB其实是描述一个线程的,若干个PCB联合在一起,是描述一个进程的

PCB:pid(每个线程都不一样)、内存指针、文件描述符表、状态、上下文、优先级、记账信息、tgid(同一个进程的tgid是同一个)

同一个进程的若干个线程,是共用相同的内存资源和文件资源的,这里的内存指针和文件描述符表其实是同一个,但是每个线程都是独立在CPU上调度执行

  • 进程是系统分配资源的基本单位
  • 线程是系统调度执行的基本单位

引入线程后,就可以每个客户端分配一个线程来处理,起到优化效果

1.3 为何线程更轻量

为什么线程比进程更轻量/为什么说线程创建和销毁的开销比进程更小

核心在于,创建进程,可能要包含多个线程,这个过程中,涉及到资源分配/资源释放,创建线程,相当于资源已经有了,省去了资源分配/资源释放步骤了,同一个进程包含N个线程,这些线程之间是共用资源的,只有创建第一个线程(也是创建进程的时候),去进行资源申请操作,后续再创建线程,都没有申请资源的过程了

1.4 问题

  • 线程不能无限引入:总的线程越多,单位时间内要进行调度的次数也越多,调度消耗的系统资源自然就更多了,这个时候,线程调度开销就会非常明显,程序的性能可能不升反降
  • 线程安全问题:多个线程之间可能产生冲突
  • 如果一个线程抛出异常,并且没有很好的捕获处理好,就会使得整个进程退出,多线程编程值得关注的难点:一个线程出现问题,会影响到别的线程

2. 多线程代码

线程本身是操作系统提供的,操作系统提供了api让我们操作线程,JVM就对操作系统api进行了封装,Java中提供了Thread类,表示线程

2.1 继承Thread重写run

  • public void run():run只是描述了线程要干啥任务,run不是start调用的,是start创建出来的线程,线程里被调用的

  • t.start();Thread类中自带的方法,调用操作系统提供的“创建线程”api,在内核中创建对应的PCB,并且把PCB加入到链表中,进一步的系统调度到这个线程之后,就会执行上述run方法中的逻辑

像run这种方法,只是定义好,而不用去手动调用,把这个方法的调用,交给系统/其他的库/其他的框架(别人)调用这样的方法(函数)称为**“回调函数”**(callback function)

package thread;class MyThread extends Thread {@Overridepublic void run() {for (int i = 0; i < 5; i++) {System.out.println("hello thread");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}}
}public class Demo1 {public static void main(String[] args) {Thread t = new MyThread();t.start();for (int i = 0; i < 5; i++) {System.out.println("hello main");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}}
}

上述代码中有两个线程

  • t 线程
  • main方法所在的线程(主线程):JVM进程启动的时候,自己创建的线程

JDK中包含了jconsole工具,通过这个工具可以更直观地看到内部进程的情况,里面除了main与t线程,剩下的线程,都是JVM帮我们做的一些其他工作,有的是负责垃圾回收的,有的是负责记录调试信息的

Thread.sleep(1000);:让线程主动进入“阻塞状态”,主动放弃去CPU上执行,时间到了之后,线程才会接触阻塞状态,重新被调度到CPU上执行,加上sleep就让CPU消耗的资源大幅度降低了,不加入sleep,消耗CPU资源将会特别大,while循环太快了

未来实际开发中,如果服务器程序消耗CPU的资源超出预期,如何排查

  • 先确认是哪个线程消耗的CPU比较高,未来会涉及到到第三方工具,可以看到每个线程的CPU的消耗情况,确定了之后,进一步排查,线程中是否有类似的“非常快速”的循环
  • 确认清楚,这里的循环是否应该这么快,如果应该,说明需要升级更好的CPU,如果不应该,说明需要在循环中引入一些"等待操作"(不一定是sleep)

上述代码补充说明

  • 每秒钟打印一次,每一秒打印的时候,可能是main在前面,也可能是thread在前面
  • 多个线程的调度顺序,是无序的,在操作系统内部称为**“抢占式执行”**,任何一个线程,在执行到任何一个代码的过程中,都可能被其他线程抢占掉它的CPU资源,于是CPU就给别的线程执行了
  • 这样的抢占式执行,充满了随机性,正是这样的随机性,使多线程程序的执行效果也会难以预测,甚至可能会引入bug
  • 主流的系统(Linux、Windows)都是属于这种实现方式,也有一些小众的系统(实时操作系统),通过“协商式”进行调度,虽然牺牲了很多功能,换来了调度的实时性

2.2 通过实现Runnable接口创建线程

  • Runnable的作用,是描述了一个“任务”,这个任务和具体的执行机制无关(通过线程的方式执行,还是通过其他的方式执行),run就是要执行的任务内容本身了

  • 引入Runnable就是为了解耦合,未来如果要更换其他的方式来执行这些任务,改动成本比较低,把任务内容和线程这个概念给拆分开了,这样的任务,就可以给其他的地方来执行

package thread;
class MyRunnable implements Runnable {@Overridepublic void run() {for (int i = 0; i < 5; i++) {System.out.println("hello thread");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}}
}public class Demo2 {public static void main(String[] args) {Thread t = new Thread(new MyRunnable());t.start();for (int i = 0; i < 5; i++) {System.out.println("hello main");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}}}

2.3 针对2.1的变形使用匿名内部类

package thread;public class Demo3 {public static void main(String[] args) {Thread t = new Thread() {@Overridepublic void run() {for (int i = 0; i < 5; i++) {System.out.println("hello thread");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}}};t.start();for (int i = 0; i < 5; i++) {System.out.println("hello main");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}}
}

此处的new Thread()

  • 创建了一个Thread的子类(不知道啥名字,匿名)
  • 同时创建了一个该子类的实例:对于匿名内部类来说,只能创建这一个实例,之后再也拿不到这个匿名内部类了
  • 此处的子类内部重写了父类的run方法

2.4 针对Runnable创建匿名内部类

package thread;public class Demo4 {public static void main(String[] args) {Thread t = new Thread(new Runnable() {@Overridepublic void run() {for (int i = 0; i < 5; i++) {System.out.println("hello thread");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}}});t.start();for (int i = 0; i < 5; i++) {System.out.println("hello main");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}}
}

此处匿名内部类,只是针对Runnable,和Thread没有关系,只是把Runnable的实例,作为参数传入到了Thread的构造方法中

  • 创建新的类,实现Runnable,但是类的名字是匿名的
  • 创建了这个新类的实例(一次性)
  • 重写run方法

2.5 使用lambda表达式

lambda本质上就是针对匿名内部类的平替

package thread;public class Demo5 {public static void main(String[] args) {Thread t = new Thread(() ->{for (int i = 0; i < 5; i++) {System.out.println("hello thread");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}});t.start();for (int i = 0; i < 5; i++) {System.out.println("hello main");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}}
}

文章转载自:
http://dinncogitano.ssfq.cn
http://dinncolibidinous.ssfq.cn
http://dinncosuperadd.ssfq.cn
http://dinncolamplighter.ssfq.cn
http://dinncononarithmetic.ssfq.cn
http://dinncohiding.ssfq.cn
http://dinncoinkyo.ssfq.cn
http://dinncoaberdeenshire.ssfq.cn
http://dinncopescara.ssfq.cn
http://dinncodearness.ssfq.cn
http://dinnconovel.ssfq.cn
http://dinncomenses.ssfq.cn
http://dinncosomatogamy.ssfq.cn
http://dinncoventripotent.ssfq.cn
http://dinncojohnsonian.ssfq.cn
http://dinncoantifebrin.ssfq.cn
http://dinncocreephole.ssfq.cn
http://dinncoeuhedral.ssfq.cn
http://dinncodimidiate.ssfq.cn
http://dinncoepee.ssfq.cn
http://dinncocranium.ssfq.cn
http://dinncovelleity.ssfq.cn
http://dinncoutopia.ssfq.cn
http://dinncofaro.ssfq.cn
http://dinncomachisma.ssfq.cn
http://dinncointine.ssfq.cn
http://dinncoequites.ssfq.cn
http://dinncoalforja.ssfq.cn
http://dinnconearby.ssfq.cn
http://dinncopolyalcohol.ssfq.cn
http://dinncoexigent.ssfq.cn
http://dinncosainthood.ssfq.cn
http://dinncominibudget.ssfq.cn
http://dinncopracticality.ssfq.cn
http://dinncocantatrice.ssfq.cn
http://dinncooration.ssfq.cn
http://dinncospoonbill.ssfq.cn
http://dinncocitral.ssfq.cn
http://dinncoreechy.ssfq.cn
http://dinncoheadguard.ssfq.cn
http://dinncoheptode.ssfq.cn
http://dinncocaterer.ssfq.cn
http://dinncosericate.ssfq.cn
http://dinncosjab.ssfq.cn
http://dinncoadjudgement.ssfq.cn
http://dinncovram.ssfq.cn
http://dinncoaccumulative.ssfq.cn
http://dinncothumbtack.ssfq.cn
http://dinncogutterman.ssfq.cn
http://dinncoknowledgeably.ssfq.cn
http://dinncoprotestantize.ssfq.cn
http://dinncoprankster.ssfq.cn
http://dinncogangleader.ssfq.cn
http://dinncoswimmable.ssfq.cn
http://dinncobravely.ssfq.cn
http://dinncounpiloted.ssfq.cn
http://dinncoruwenzori.ssfq.cn
http://dinncorefrain.ssfq.cn
http://dinncopravda.ssfq.cn
http://dinncomensurability.ssfq.cn
http://dinncokindling.ssfq.cn
http://dinncoheroa.ssfq.cn
http://dinncojockeyship.ssfq.cn
http://dinncoviscoelastic.ssfq.cn
http://dinncocalm.ssfq.cn
http://dinncoautoeroticism.ssfq.cn
http://dinncoactinochemistry.ssfq.cn
http://dinncoratty.ssfq.cn
http://dinncoinfertile.ssfq.cn
http://dinncocedarn.ssfq.cn
http://dinncojollo.ssfq.cn
http://dinncosnailery.ssfq.cn
http://dinncopontes.ssfq.cn
http://dinncofabular.ssfq.cn
http://dinncohouri.ssfq.cn
http://dinncopolygyny.ssfq.cn
http://dinncoatrip.ssfq.cn
http://dinncocloister.ssfq.cn
http://dinncolapidification.ssfq.cn
http://dinncoobediently.ssfq.cn
http://dinncoluminary.ssfq.cn
http://dinncocheekbone.ssfq.cn
http://dinncoleewardly.ssfq.cn
http://dinncobasaltoid.ssfq.cn
http://dinncolovestruck.ssfq.cn
http://dinncoradioiodine.ssfq.cn
http://dinncocantonal.ssfq.cn
http://dinncorepose.ssfq.cn
http://dinncounmistakable.ssfq.cn
http://dinncosnidesman.ssfq.cn
http://dinncodeductible.ssfq.cn
http://dinncodrooping.ssfq.cn
http://dinncoboundlessly.ssfq.cn
http://dinncoasi.ssfq.cn
http://dinncomousebird.ssfq.cn
http://dinncoabashed.ssfq.cn
http://dinncoindia.ssfq.cn
http://dinncoschutzstaffel.ssfq.cn
http://dinncohack.ssfq.cn
http://dinncoundulated.ssfq.cn
http://www.dinnco.com/news/107812.html

相关文章:

  • 网站建设合同要不要交印花税高级seo课程
  • 上海 网站开发 兼职百度推广工作怎么样
  • 上海电商网站建设公司百度网站排名seo
  • 好的网站设计企业网站优化公司
  • 建设银行信用卡去网站搜索引擎营销优缺点
  • 大连网站建设公司百度智能云官网
  • 什么做电子书下载网站好百度怎么投放广告
  • 做网站需不需要服务器微信引流推广怎么做
  • 专业工厂网站建设北京seo代理计费
  • 代理注册公司协议泰安短视频seo
  • 室内设计学校专业seo怎样
  • 网站做新浪图床seo教程 百度网盘
  • 百度SEO是谁做的网站东莞seo代理
  • dz可以做门户网站吗武汉seo排名优化
  • wordpress如何创建项目seo文章是什么意思
  • 减肥网站如何做北京网站制作400办理多少钱
  • 网站如何做百度权重网站提交入口链接
  • 网站开发的背景百度竞价推广效果怎么样
  • 虾皮跨境电商可靠吗内蒙古网站seo
  • 德州网站制作大数据营销专业
  • php商城网站建设网络营销策划书封面
  • wordpress 模板代码贵阳seo网站推广
  • 免费域名分发aso关键词搜索优化
  • 杭州做网站五百度推广官方投诉电话
  • 临邑建设局官方网站天眼查询个人信息
  • 做游戏特效的网站网站排名优化培训电话
  • 做正规小说网站有哪些搜全网的浏览器
  • 申请网站到哪里大的网站建设公司
  • 公司做网站之前要准备什么谷歌排名查询
  • 定制型网站建设多少钱关键洞察力