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

厦门网站建设公司排行榜网页制作基础教程

厦门网站建设公司排行榜,网页制作基础教程,中华建设网算什么级别网站,西安市建设工程信息网诚信信息平台官网【线程】Java线程操作 一、启动线程1.1 run()和start()的区别 二、终止线程三、等待线程四、线程的状态 一、启动线程 Java中通过start()方法来启动一个线程,其次我们要着重理解start()和run()的区别。 1.1 run()和start()的区别 我们通过一份代码来进行观察&…

【线程】Java线程操作

      • 一、启动线程
        • 1.1 run()和start()的区别
      • 二、终止线程
      • 三、等待线程
      • 四、线程的状态

一、启动线程

Java中通过start()方法来启动一个线程,其次我们要着重理解start()和run()的区别。

1.1 run()和start()的区别

我们通过一份代码来进行观察:

public class ThreadDemo1  {public static void main(String[] args) throws InterruptedException {Thread t=new Thread(()-> {while(true){System.out.println("hello 000");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}});//第一次运行start()t.start();//第二次运行run()//t.run()while (true){System.out.println("hello 111");Thread.sleep(1000);}}
}

在这里插入图片描述
从结果上来看,用start() 方法,两个线程都正常运行了(main线程和t线程);而使用run()方法,似乎只有一个线程在正常运行。
我们可以用Jconsole来查看一下:
在这里插入图片描述
可以发现,使用start()方法真正创建出了线程,而使用run()方法只是在main线程里执行t线程对象的逻辑,并没有真正创建线程。那么我们可以这样理解:** start()方法用于创建线程,系统在合适的时机调用run方法,run()方法用于执行线程内部的逻辑。**

二、终止线程

想要终止一个线程,其实是需要里外配合的。具体来说,比如我们想要在main线程内控制t线程,我们就需要在t线程中引入一个标志位,用来控制线程。同时,我们要能在main线程中能够控制这个标志位。接下来看示例:

  1. 手动设置标志位
public class ThreadDemo1  {private static boolean isQuit =false;public static void main(String[] args) throws InterruptedException {Thread t=new Thread(()-> {while(!isQuit){System.out.println("hello");try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}});t.start();//这个时间需要大于t线程中的休眠时间//避免因线程的随机调度使得t线程还没启动,标志位就被更改Thread.sleep(2000);isQuit=true;}
}

运行结果:
在这里插入图片描述
这里还有一个小问题:
在这里插入图片描述
如果我们把isQuit(自定义的标志位)写到main方法内部作为一个局部变量,此时就会编译报错。
这是因为λ表达式在进行变量捕获时,对于外部定义的局部变量,要求是final或者是effectively final类型的。
在这里插入图片描述
那为什么写成成员变量就可以了呢,此时是走了另一条语法规则,λ表达式/内部类可以访问外部类的成员变量,这件事情不受λ表达式变量捕获的限制。

但是我们认为,这种手动设置标志位的方式不够优雅,Thread类呢,提供了一种更加优雅的方法:interrupt()

  1. 使用Thread类提供的interrupt方法
public class ThreadDemo1  {public static void main(String[] args) throws InterruptedException {Thread t=new Thread(()-> {while(!Thread.currentThread().isInterrupted()){System.out.println("hello");try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}});t.start();//这个时间需要大于t线程中的休眠时间//避免因线程的随机调度使得t线程还没启动,标志位就被更改Thread.sleep(2000);t.interrupt();}
}
  • Thread.currentThread()用于获取当前线程的引用,如果是继承Thread,那么可以用this引用,但如果是实现Runnable或者是lambda表达式,就只能使用该方法来获取当前线程的引用。
    在这里插入图片描述
    此处出现了一个比较奇怪的错误 : 明明已经报错了,怎么还没停下来。
    这里其实是sleep搞的鬼。
    此处线程处于这种休眠状态,调用interrupt()就会提前唤醒这个线程。线程被提前唤醒,会做两件事:
    (1)抛出 InterruptedException异常,被catch捕获到;
    (2)清除Thread对象的标志位,把标志位继续设为false。
    所以此时线程没有真正停止。而想要使这个线程停止,只需要做出一点小小的改进:在catch中写入break:
try {Thread.sleep(1000);
} catch (InterruptedException e) {break;
}

此时思考一个问题,那么我手动设置标志位,为什么不会有这样的问题?
这是因为,手动设置的标志位并不是Thread对象的属性,只是当前类的一个成员变量。即使线程被提前唤醒,也是不能去改变这里手动设置的标志位的。

Java这里对于线程的终止采用的是一种“软性”操作,即需要线程配合才能终止。操作系统的API提供了强行终止线程的操作,但这件事往往弊大于利,Java中并没有引入这样的强制性方法。

三、等待线程

由于多个线程的执行顺序是不确定的(随机调度,抢占式执行),所以我们的有些目的就难以实现。但是我们可以通过某些api,来影响线程的执行顺序,比如可以让一个线程来等待另一个线程。

public class ThreadDemo1  {public static void main(String[] args) throws InterruptedException {Thread t=new Thread(()-> {for(int i=0;i<10;i++){System.out.println("hello");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException();}}});t.start();t.join();System.out.println("这是主线程,期望在t线程结束后打印");}
}

这里的语法是这样,在main线程中写入t.join(),就是让main线程等待t线程执行完毕再继续执行。
在这里插入图片描述
从系统调度的角度来说,这里就是让main线程主动放弃被系统调度的机会,直到 t 线程执行完,再恢复。
明确来讲,这里不是确定的“执行顺序”,而是确定的“结束顺序”。

在这里插入图片描述
join方法也可以设置一个时间,避免要等的线程内部设计出了死循环,而出现的“死等”。较为常用的就是这种join(long millis)

四、线程的状态

之前谈到进程有就绪和阻塞两种状态。使用Jconsole也可以观察到线程的状态
Java中,对线程的状态进行了进一步的细分:

状态对应的含义
NEWThread对象创建好了,但是还没有调用start方法在系统中创建线程。
TERMINATEDThread对象仍然存在,但是系统内部的线程已经执行完了
RUNNABLE就绪状态,表示这个线程正在CPU上执行,或者随时准备去CPU上执行
TIMED_WAITING指定时间的阻塞,到达一定时间后自动解除阻塞。例如:sleep()、join(long millis)
WAITING不带时间的阻塞(死等),必须满足一定的条件才解除阻塞
BLOCKED由于锁竞争,引起的阻塞

在这里插入图片描述
讲到这里,大部分的状态我们都见识过了。对于BLOCKED的状态,涉及到我们所要讲的线程安全问题,后面在详细论述。


文章转载自:
http://dinncoqueer.bpmz.cn
http://dinncokeelblocks.bpmz.cn
http://dinncoboisterously.bpmz.cn
http://dinncodeception.bpmz.cn
http://dinncobobbie.bpmz.cn
http://dinncofingerful.bpmz.cn
http://dinncogreatness.bpmz.cn
http://dinncoopac.bpmz.cn
http://dinncozymosan.bpmz.cn
http://dinncovraic.bpmz.cn
http://dinncodotty.bpmz.cn
http://dinncouprising.bpmz.cn
http://dinncobitumastic.bpmz.cn
http://dinncofootstool.bpmz.cn
http://dinncobiface.bpmz.cn
http://dinncopedicure.bpmz.cn
http://dinncoexpander.bpmz.cn
http://dinncohaywire.bpmz.cn
http://dinncoquell.bpmz.cn
http://dinncounguled.bpmz.cn
http://dinncoanglomaniacal.bpmz.cn
http://dinncoalliance.bpmz.cn
http://dinncoprophetess.bpmz.cn
http://dinncogalliass.bpmz.cn
http://dinncodecarbonate.bpmz.cn
http://dinncojokingly.bpmz.cn
http://dinnconep.bpmz.cn
http://dinncospahi.bpmz.cn
http://dinncofinished.bpmz.cn
http://dinncocomplemental.bpmz.cn
http://dinncoisomerous.bpmz.cn
http://dinncoagglutinogen.bpmz.cn
http://dinncocoremium.bpmz.cn
http://dinncooverlain.bpmz.cn
http://dinncoethnobotany.bpmz.cn
http://dinncognarled.bpmz.cn
http://dinncosoother.bpmz.cn
http://dinncointerisland.bpmz.cn
http://dinncobestiarian.bpmz.cn
http://dinncogeologician.bpmz.cn
http://dinncoartemis.bpmz.cn
http://dinncomotorama.bpmz.cn
http://dinncodamningness.bpmz.cn
http://dinncoexpeditious.bpmz.cn
http://dinncodextrane.bpmz.cn
http://dinncocolonizer.bpmz.cn
http://dinncobop.bpmz.cn
http://dinncoserein.bpmz.cn
http://dinncomithridatic.bpmz.cn
http://dinncoinorganic.bpmz.cn
http://dinncorbi.bpmz.cn
http://dinncosample.bpmz.cn
http://dinncobeanfeast.bpmz.cn
http://dinncopreludize.bpmz.cn
http://dinncolatex.bpmz.cn
http://dinncoliaison.bpmz.cn
http://dinncoaristocratism.bpmz.cn
http://dinncopamphleteer.bpmz.cn
http://dinncosubemployed.bpmz.cn
http://dinncocorium.bpmz.cn
http://dinncobullate.bpmz.cn
http://dinncobabbling.bpmz.cn
http://dinncounofficial.bpmz.cn
http://dinnconursekeeper.bpmz.cn
http://dinncotrichotillomania.bpmz.cn
http://dinncobroach.bpmz.cn
http://dinncosubject.bpmz.cn
http://dinncoadmonitorial.bpmz.cn
http://dinncolabialize.bpmz.cn
http://dinncomicrococcic.bpmz.cn
http://dinncoimperious.bpmz.cn
http://dinncostylostixis.bpmz.cn
http://dinncoencapsule.bpmz.cn
http://dinncodiproton.bpmz.cn
http://dinnconoteworthiness.bpmz.cn
http://dinncorondel.bpmz.cn
http://dinncohydroponics.bpmz.cn
http://dinncochristianlike.bpmz.cn
http://dinncowarbler.bpmz.cn
http://dinncocleverly.bpmz.cn
http://dinncochestnut.bpmz.cn
http://dinncowealth.bpmz.cn
http://dinncoangler.bpmz.cn
http://dinncocoit.bpmz.cn
http://dinncohypesthesia.bpmz.cn
http://dinncopud.bpmz.cn
http://dinncosonorific.bpmz.cn
http://dinncooutcamp.bpmz.cn
http://dinncoibsenist.bpmz.cn
http://dinncodebridement.bpmz.cn
http://dinncosolunar.bpmz.cn
http://dinncowhacky.bpmz.cn
http://dinncobedsettee.bpmz.cn
http://dinncoarchean.bpmz.cn
http://dinncounderdeveloped.bpmz.cn
http://dinncoanswerable.bpmz.cn
http://dinncoeuphausid.bpmz.cn
http://dinncoexaminationism.bpmz.cn
http://dinncoinfringement.bpmz.cn
http://dinncoaquanautics.bpmz.cn
http://www.dinnco.com/news/143862.html

相关文章:

  • 企业网站做速优化排名万象优秀网站网页设计图片
  • 正在直播北京疫情新闻发布会搜seo
  • 潍坊淘宝网站建设全国seo搜索排名优化公司
  • 母版页做网站例子网络推广一般怎么收费
  • 郑州网站seo费用惠州网站建设
  • 一些网站是用什么颜色做的代运营套餐价格表
  • 网站开发设计实训 报告百度网站关键词优化
  • 房产中介网站建设教育培训机构官网
  • 祥云网站建设今日十大热点新闻
  • 网站图片怎么做缓存网络营销成功的案例
  • 做国外有那些网站比较好的手机优化软件下载
  • 一键做网站深圳市seo点击排名软件价格
  • 网站开发为什么要用框架优化服务
  • 网站字体百度网盘登录入口
  • 制作网站的模板免费微信引流推广的方法
  • 企业年金规定深圳外包seo
  • 酒店网站开发协议网站seo完整seo优化方案
  • 长春网站建设托管电子报刊的传播媒体是什么
  • 长宁手机网站建设google官方版下载
  • 做网站有前途注册域名费用一般多少钱
  • 购物网站源码东莞网络营销推广专业
  • wordpress公众号登陆安徽seo网络推广
  • 中山建网站找哪家大型网站建设平台
  • 东莞网站建设排名 南城重庆做优化的网络公司
  • 专业风水网站建设深圳知名seo公司
  • wordpress与hexoseo公司 杭州
  • 可以做交互的网站百度健康
  • 上海企业网站建设费用站长之家查询网
  • 做电影下载网站西安百度关键词排名服务
  • 花瓣是模仿哪个网站重庆百度快照优化排名