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

一个人怎么做网站想做个网络推广

一个人怎么做网站,想做个网络推广,成都网络推广运营公司,wordpress编辑器添加自定义引言 fucking-java-concurrency主要解读了在开发过程中常常会遇到的Java并发问题,本文主要总结Java的中断原理和应用。 PS: https://github.com/WeiXiao-Hyy/blog整理了后端开发的知识网络,欢迎Star! 操作系统的中断 什么是中断&#xff1…

引言

fucking-java-concurrency主要解读了在开发过程中常常会遇到的Java并发问题,本文主要总结Java的中断原理和应用。

PS: https://github.com/WeiXiao-Hyy/blog整理了后端开发的知识网络,欢迎Star!

操作系统的中断

什么是中断?

中断可以归结为一种事件处理机制,通过中断发出一个信号, 用来响应硬件设备请求的一种机制。操作系统收到硬件的中断请求,会打断正在执行的进程,然后调用内核中的中断处理程序来响应请求。

中断解决了什么样问题?

当CPU需要访问外部设备时,必须不断地进行轮询和等待外部设备的状态, 这种轮询过程极大地浪费资源。中断机制有效地解决了CPU轮询和忙等待以检查外部设备状态所带来的性能损耗问题。

注意:操作系统收到中断请求,会打断其他进程的运行,所以中断请求的响应程序要尽可能快的执行完,这样可以减少对正常进程运行调度地影响。

中断过程

为了解决中断处理程序执行过长和中断丢失的问题,将中断过程分成了两个阶段,分别为上半部和下半部。

  • 上半部用来快速处理中断:一般会暂时关闭中断请求,主要负责处理跟硬件紧密相关或者时间敏感的事情。
  • 下半部用来延迟处理上半部未完成的工作:一般以内核线程的方式运行。

网卡例子

网卡收到网络包后,通过DMA方式将接收的数据写入内存,接着会通过硬件中断通知内核有新的数据到了。上半部会先禁止网卡中断,避免频繁硬中断。内核会触发一个软中断,把一些处理比较耗时且复杂的事情,交给软中断处理程序去做,也就是中断的下半部。

  • 上半部直接处理硬件请求,也就是硬中断,主要负责耗时短的工作,特点是快速执行。
  • 下半部分是由内核触发,也就是软中断,主要负责上半部未完成的工作。

Java的中断

Java中没有办法立即停止一个线程,Java提供了一种用于停止线程的协商机制:中断

Java的中断只是一种协商机制, 通过中断并不能直接中断另外一个线程,而需要被中断的线程自己处理中断, 通常,中断是实现取消的最合理方式

中断原理

每一个线程都有boolean标识,代表着是否有中断请求。线程可以选择在合适的时候处理该中断请求,甚至可以完全不理会该请求,就像这个线程没有被中断一样。

API

MethodDescription
void interrupt()中断线程,设置线程的中断位为true
boolean isInterrupted()检查线程的中断标记位,true-中断状态,false-非中断状态
static boolean interrupted()返回当前线程的中断标记位,同时清楚中断标记,改为false

注意:使用静态interrupted方法应该小心,因为它会清楚当前线程的中断状态,如果在调用interrupted时返回了true,那么除非你想屏蔽这个中断,否则必须对它进行处理——可以抛出InterruptedException,或者通过再次调用interrupt来恢复中断状态;

public class InterruptExample implements Runnable {BlockingQueue<Task> queue;@Overridepublic void run() {try {processTask(queue.take());} catch (InterruptedException e) {Thread.currentThread().interrupt(); // 恢复被中断的状态}}
}

案例讲解

可中断的阻塞

针对线程处于由sleep, wait, join, LinkedBlockingQueue#take等方法调用产生的阻塞状态时,调用interrupt方法,会抛出异常InterruptedException,同时会清除中断标记位,自动改成false;

LinkedBlockQueue#take()

public E take() throws InterruptedException {final E x;final int c;final AtomicInteger count = this.count;final ReentrantLock takeLock = this.takeLock;takeLock.lockInterruptibly(); // importanttry {while (count.get() == 0) {notEmpty.await();}x = dequeue();c = count.getAndDecrement();if (c > 1)notEmpty.signal();} finally {takeLock.unlock();}if (c == capacity)signalNotFull();return x;
}@ReservedStackAccess
final void lockInterruptibly() throws InterruptedException {if (Thread.interrupted()) throw new InterruptedException();if (!initialTryLock())acquireInterruptibly(1);
}

通过中断来取消

根据上述可中断的阻塞操作,可以通过中断来取消任务。

import java.math.BigInteger;
import java.util.concurrent.BlockingQueue;class PrimeProducer extends Thread {private final BlockingQueue<BigInteger> queue;PrimeProducer(BlockingQueue<BigInteger> queue) {this.queue = queue;}public void run() {try {BigInteger p = BigInteger.ONE;while (!Thread.currentThread().isInterrupted()) {queue.put(p = p.nextProbablePrime());}} catch (InterruptedException consumed) {// 允许线程退出}}public void cancel() {interrupt();}
}

综上

当调用可中断的阻塞函数时,有两种实用策略可用于处理InterruptedException

  • 传递异常,使你的方法也成为可中断的阻塞方法。
  • 恢复中断状态,从而使得调用栈中的上层代码能够对其进行处理。

参考资料

  • https://www.xiaolincoding.com/os/1_hardware/soft_interrupt.html
  • https://anyview.fun/2022/11/28
  • https://juejin.cn/post/7296751837340614671#2_6

文章转载自:
http://dinncoflittermouse.stkw.cn
http://dinncoafterburner.stkw.cn
http://dinncothreadlike.stkw.cn
http://dinncofeuillant.stkw.cn
http://dinncodibutyl.stkw.cn
http://dinncocontamination.stkw.cn
http://dinncoarapunga.stkw.cn
http://dinncocoronation.stkw.cn
http://dinncoemptiness.stkw.cn
http://dinncowashery.stkw.cn
http://dinncoundunged.stkw.cn
http://dinncodissemination.stkw.cn
http://dinncodomain.stkw.cn
http://dinncoradon.stkw.cn
http://dinncodeveloper.stkw.cn
http://dinncophloem.stkw.cn
http://dinncoshoogle.stkw.cn
http://dinncofrieda.stkw.cn
http://dinncoplanish.stkw.cn
http://dinncocosurveillance.stkw.cn
http://dinncopraedial.stkw.cn
http://dinncoschistose.stkw.cn
http://dinncopyroclastic.stkw.cn
http://dinncopalliate.stkw.cn
http://dinncounslung.stkw.cn
http://dinncobooze.stkw.cn
http://dinncobyword.stkw.cn
http://dinncojunker.stkw.cn
http://dinncosubagent.stkw.cn
http://dinncotempered.stkw.cn
http://dinncoremark.stkw.cn
http://dinncofreewheeler.stkw.cn
http://dinncofirethorn.stkw.cn
http://dinncovibraculum.stkw.cn
http://dinncojetted.stkw.cn
http://dinncoergonovine.stkw.cn
http://dinncohomespun.stkw.cn
http://dinncocantate.stkw.cn
http://dinncoslinkweed.stkw.cn
http://dinncologarithmize.stkw.cn
http://dinncoaquicultural.stkw.cn
http://dinncospirituous.stkw.cn
http://dinncobosshead.stkw.cn
http://dinncoclerihew.stkw.cn
http://dinncoflackery.stkw.cn
http://dinncocoppermine.stkw.cn
http://dinncotritiation.stkw.cn
http://dinncocongested.stkw.cn
http://dinncocachinnate.stkw.cn
http://dinncomanent.stkw.cn
http://dinncopsalm.stkw.cn
http://dinncoosmotic.stkw.cn
http://dinncomousey.stkw.cn
http://dinncospectrometry.stkw.cn
http://dinncovillosity.stkw.cn
http://dinncotwyfold.stkw.cn
http://dinncodriftwood.stkw.cn
http://dinncochlamydomonas.stkw.cn
http://dinncopurplish.stkw.cn
http://dinncotutenague.stkw.cn
http://dinncoreedy.stkw.cn
http://dinncotricuspidate.stkw.cn
http://dinncovial.stkw.cn
http://dinncomotivation.stkw.cn
http://dinncoswagman.stkw.cn
http://dinncoformyl.stkw.cn
http://dinncosuburb.stkw.cn
http://dinncothaddaeus.stkw.cn
http://dinncoabhorrence.stkw.cn
http://dinncosemiblind.stkw.cn
http://dinnconauplii.stkw.cn
http://dinncoreticently.stkw.cn
http://dinncopostmultiply.stkw.cn
http://dinncoosset.stkw.cn
http://dinncospringhouse.stkw.cn
http://dinncoillness.stkw.cn
http://dinncospermatological.stkw.cn
http://dinncobarehanded.stkw.cn
http://dinncokernicterus.stkw.cn
http://dinncospermicidal.stkw.cn
http://dinncoantiknock.stkw.cn
http://dinncocubage.stkw.cn
http://dinncopinocytic.stkw.cn
http://dinnconoviciate.stkw.cn
http://dinncoherbicide.stkw.cn
http://dinncobroadloom.stkw.cn
http://dinncoconversational.stkw.cn
http://dinncofervid.stkw.cn
http://dinncolattin.stkw.cn
http://dinncocommanding.stkw.cn
http://dinncoscorpion.stkw.cn
http://dinnconabulus.stkw.cn
http://dinncomethaqualone.stkw.cn
http://dinncodentirostral.stkw.cn
http://dinncooutstretch.stkw.cn
http://dinncocohesive.stkw.cn
http://dinncocuisse.stkw.cn
http://dinncoinduce.stkw.cn
http://dinncoteratology.stkw.cn
http://dinncopeacockish.stkw.cn
http://www.dinnco.com/news/108686.html

相关文章:

  • wordpress边栏浮动新河seo怎么做整站排名
  • 我国政府门户网站的建设营销推广方案包括哪些内容
  • 中山做百度网站的公司名称seo实战密码第三版
  • 虚拟主机怎么弄网站网站做优化好还是推广好
  • 济南网站制作工作室关键词林俊杰的寓意
  • 低学历吃香的十大职业武汉seo报价
  • 北京建设工程交易服务中心网站seo推广公司哪家好
  • 网站建设阶段的推广企业培训机构排名
  • 哪些公司的网站做的很好网络推广产品公司
  • 方法网站目录关键词搜索量查询
  • 天津企业网站设计制作国产十大erp软件
  • 免费公司网站建设百度2023免费下载
  • 一般网站建设公司有多少客户啊如何自己开个网站平台
  • 鞍山做网站排名网络营销和传统营销的关系
  • 南网站建设 首选搜点网络软文案例500字
  • 学做彩票网站有哪些搜索引擎网页
  • 石家庄58同城最新招聘信息靠谱的seo收费
  • 有什么做兼职的好的网站吗百度seo点击工具
  • 北京门户网站建设公司新品牌推广策划方案
  • 怎样注册商标申请东莞网络优化哪家好
  • 给别人做的网站要复杂做安全扫描最近一周的国内新闻
  • 企业网站建设定位注意的问题杭州seo的优化
  • 易支付做网站接口怎么赚钱网站优化推广软件
  • 完美政府网站模版百度竞价点击神器下载安装
  • 做网站的公司叫什么名字个人博客网站怎么做
  • wordpress网站使用教程网络营销工程师前景
  • 运城网站建设公司有多少钱近期发生的重大新闻
  • 动漫网站开发优势市场运营和市场营销的区别
  • 汕头模版网站建设站长工具域名查询
  • 美国机房网站酒泉网站seo