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

专业做网盘资源收录分享的网站百度广告投放价格表

专业做网盘资源收录分享的网站,百度广告投放价格表,千万不要进住建局,专门做特医食品的网站文章目录 4.2 启动和终止线程4.2.1 构造线程4.2.2 启动线程4.2.3 理解中断4.2.4 过期的suspend()、resume()和stop()4.2.5 安全地终止线程 4.2 启动和终止线程 在前面章节的示例中通过调用线程的start()方法进行启动,随着run()方法的执行完毕,线程也随之…

文章目录

  • 4.2 启动和终止线程
    • 4.2.1 构造线程
    • 4.2.2 启动线程
    • 4.2.3 理解中断
    • 4.2.4 过期的suspend()、resume()和stop()
    • 4.2.5 安全地终止线程

4.2 启动和终止线程

在前面章节的示例中通过调用线程的start()方法进行启动,随着run()方法的执行完毕,线程也随之终止,大家对此一定不会陌生,下面将详细介绍线程的启动和终止。

4.2.1 构造线程

在运行线程之前首先要构造一个线程对象,线程对象在构造的时候需要提供线程所需要的属性,如线程所属的线程组、线程优先级、是否是Daemon线程等信息。以下代码摘自java.lang.Thread 中对线程进行初始化的部分。

    /*** Initializes a Thread.** @param g the Thread group* @param target the object whose run() method gets called* @param name the name of the new Thread* @param stackSize the desired stack size for the new thread, or*        zero to indicate that this parameter is to be ignored.* @param acc the AccessControlContext to inherit, or*            AccessController.getContext() if null* @param inheritThreadLocals if {@code true}, inherit initial values for*            inheritable thread-locals from the constructing thread*/private void init(ThreadGroup g, Runnable target, String name,long stackSize, AccessControlContext acc,boolean inheritThreadLocals) {if (name == null) {throw new NullPointerException("name cannot be null");}this.name = name;Thread parent = currentThread();SecurityManager security = System.getSecurityManager();if (g == null) {/* Determine if it's an applet or not *//* If there is a security manager, ask the security managerwhat to do. */if (security != null) {g = security.getThreadGroup();}/* If the security doesn't have a strong opinion of the matteruse the parent thread group. */if (g == null) {g = parent.getThreadGroup();}}/* checkAccess regardless of whether or not threadgroup isexplicitly passed in. */g.checkAccess();/** Do we have the required permissions?*/if (security != null) {if (isCCLOverridden(getClass())) {security.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);}}g.addUnstarted();this.group = g;this.daemon = parent.isDaemon();this.priority = parent.getPriority();if (security == null || isCCLOverridden(parent.getClass()))this.contextClassLoader = parent.getContextClassLoader();elsethis.contextClassLoader = parent.contextClassLoader;this.inheritedAccessControlContext =acc != null ? acc : AccessController.getContext();this.target = target;setPriority(priority);if (inheritThreadLocals && parent.inheritableThreadLocals != null)this.inheritableThreadLocals =ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);/* Stash the specified stack size in case the VM cares */this.stackSize = stackSize;/* Set thread ID */tid = nextThreadID();}

在上述过程中,一个新构造的线程对象是由其parent线程来进行空间分配的,而child线程继承丁parent是否为Daemon、优先级和加载资源的contextClassLoader以及可继承的ThreadLocal、同时还会分配一个唯一的ID来标识这个 child线程。至此,一个能够运行的线程对象就初始化好了,在堆内存中等待着运行。

4.2.2 启动线程

线程对象在初始化完成之后,调用start()方法就可以启动这个线程。线程start()方法含义是:当前线程(即parent线程)同步告知Java虚拟机,只要线程规划器空闲,应立即动调用 start()方法的线程。

注意:启动一个线程前,最好为这个线程设置线程名称,因为这样在使用jstack分析程序者进行问题排查时,就会给开发人员提供一些提示,自定义的线程最好能够起个名字。

4.2.3 理解中断

中断可以理解为线程的一个标识位属性,它表示一个运行中的线程是否被其他线程进行了中断操作。中断好比其他线程对该线程打了个招呼,其他线程通过调用该线程的interrupt()方法对其进行中断操作。

线程通过检查自身是否被中断来进行响应,线程通过方法isInterrupted()来进行判断否被中断,也可以调用静态方法Thread.interupted()对当前线程的中断标识位进行复位。如果该线程已经处于终结状态,即使该线程被中断过,在调用该线程对象的isInterrupted()时依旧会返回 false。

从Java的API中可以看到,许多声明抛出InterruptedException的方法(例如Thread.sleep(long millis)方法),这些方法InterruptedException之前,Java虚拟机会先将该线程的中断标识位清除,然后抛出InterruptedException,此时调用isInterrupted()方法将会返回false。

在下面代码中,首先创建了两个线程,SleepThread和BusyThread,前者不停地睡眠,后者一直运行,然后对这两个线程分别进行中断操作,观察二者的中断标识位。


import java.util.concurrent.TimeUnit;public class Interrupted {public static void main(String[] args) throws InterruptedException {// sleepThread不停地尝试睡眠Thread sleepThread = new Thread(new SleepRunner(), "SleepThread");sleepThread.setDaemon(true);// busyThread不停地运行Thread busyThread = new Thread(new BusyRunner(), "BusyThread");busyThread.setDaemon(true);sleepThread.start();busyThread.start();// 休眠5秒,让sleepThread和busyThread充分运行TimeUnit.SECONDS.sleep(5);sleepThread.interrupt();busyThread.interrupt();System.out.println("SleepThread interrupted is " + sleepThread.isInterrupted());System.out.println("BusyThread interrupted is " + busyThread.isInterrupted());// 防止sleepThread和busyThread立刻退出SleepUtils.second(2);}static class SleepRunner implements Runnable {@Overridepublic void run() {while (true) {SleepUtils.second(10);}}}static class BusyRunner implements Runnable {@Overridepublic void run() {while (true) {}}}
}
import java.util.concurrent.TimeUnit;public class SleepUtils {public static final void second(long seconds) {try {TimeUnit.SECONDS.sleep(seconds);} catch (InterruptedException e) {e.printStackTrace();}}
}

打印:

SleepThread interrupted is false
BusyThread interrupted is true
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at java.lang.Thread.sleep(Thread.java:340)
at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386)
at com.xin.demo.threaddemo.bookdemo.SleepUtils.second(SleepUtils.java:8)
at com.xin.demo.threaddemo.bookdemo.Interrupted$SleepRunner.run(Interrupted.java:32)
at java.lang.Thread.run(Thread.java:748)

从结果可以看出,抛出InterruptedException的线程SleepThread,其中断标识位被清除了,而一直忙碌运作的线程BusyThread,中断标识位没有被清除。

4.2.4 过期的suspend()、resume()和stop()

大家对于CD机肯定不会陌生,如果把它播放音乐比作一个线程的运作,那么对音乐播放做出的暂停、恢复和停止操作对应在线程Thread的API就是suspend()、resume()和stop()。

在下面代码中,创建了一个线程PrintThread,它以1秒的频率进行打印,而主线程对其进行暂停、恢复和停止操作。


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;public class Deprecated {public static void main(String[] args) throws Exception {DateFormat format = new SimpleDateFormat("HH:mm:ss");Thread printThread = new Thread(new Runner(), "printThread");printThread.setDaemon(true);printThread.start();TimeUnit.SECONDS.sleep(3);// 将PrintThread进行暂停,输出内容工作停止printThread.suspend();System.out.println("main suspend PrintThread at " + format.format(new Date()));TimeUnit.SECONDS.sleep(3);// 将PrintThread进行恢复,输出内容继续printThread.resume();System.out.println("main resume PrintThread at " + format.format(new Date()));TimeUnit.SECONDS.sleep(3);//将PrintThread进行终止,输出内容停止printThread.stop();System.out.println("main stop PrintThread at " + format.format(new Date()));TimeUnit.SECONDS.sleep(3);}static class Runner implements Runnable {@Overridepublic void run() {DateFormat format = new SimpleDateFormat("HH:mm:ss");while (true) {System.out.println(Thread.currentThread().getName() + " Run at " + format.format(new Date()));SleepUtils.second(1);}}}
}

打印:

printThread Run at 20:46:36
printThread Run at 20:46:37
printThread Run at 20:46:38
main suspend PrintThread at 20:46:39
main resume PrintThread at 20:46:42
printThread Run at 20:46:42
printThread Run at 20:46:43
printThread Run at 20:46:44
main stop PrintThread at 20:46:45

在执行过程中,PrintThread运行了3秒,随后被暂停,3秒后恢复,最后经过3秒被终止。通过示例的输出可以看到,suspend()、resume()和stop()方法完成了线程的暂停、恢复和终止工作,而且非常“人性化”。

不建议使用的原因主要有:以suspend()方法为例,在调用后,线程不会释放已经占有的资源(比如锁),而是占有着资源进入睡眠状态,这样容易引发死锁问题。同样,stop()方法在终结一个线程时不会保证线程的资源正常释放。通常是没有给予线程完成资源释放工作的机会,因此会导致程序可能工作在不确定状态下。

注意:正因为 suspend()、resume()和stop()方法带来的副作用,这些方法才被标注为不建议使用的过期方法,而暂停和恢复操作可以用后面提到的等待/通知机制来替代。

4.2.5 安全地终止线程

在4.2.3节中提到的中断状态是线程的一个标识位,而中断操作是一种简便的线程间交互方式,而这种交互方式最适合用来取消或停止任务。除了中断以外,还可以利用一个boolean 变量来控制是否需要停止任务并终止该线程。

在下面代码中,创建了一个线程CountThread,它不断地进行变量累加,面主线程尝试对其进行中断操作和停止操作。

import java.util.concurrent.TimeUnit;public class ShutDown {public static void main(String[] args) throws InterruptedException {Runner one = new Runner();Thread countThread = new Thread(one, "CountThread");countThread.start();// 睡眠1秒,main线程对CountThread进行中断,使CountThread能够感知中断而结束TimeUnit.SECONDS.sleep(1);countThread.interrupt();Runner two = new Runner();countThread = new Thread(two, "CountThread");countThread.start();// 睡眠1秒,main线程对Runner two进行取消,使CountThread能够感知on为false而结束TimeUnit.SECONDS.sleep(1);two.cancel();}private static class Runner implements Runnable {private long i;private volatile boolean on = true;@Overridepublic void run() {while (on && !Thread.currentThread().isInterrupted()) {i++;}System.out.println("Count i = " + i);}public void cancel() {on = false;}}
}

打印:

Count i = 198875747
Count i = 196373773

示例在执行过程中,main线程通过中断操作和cancel()方法均可使CountThread得以终止。这种通过标识位或者中断操作的方式能够使线程在终止时有机会去清理资源,而不是武断地将线程停止,因此这种终止线程的做法显得更加安全和优雅。


文章转载自:
http://pubescent.sfwd.cn
http://distolingual.sfwd.cn
http://noncooperativity.sfwd.cn
http://dyspnea.sfwd.cn
http://bequest.sfwd.cn
http://cingulotomy.sfwd.cn
http://scruple.sfwd.cn
http://canister.sfwd.cn
http://avionics.sfwd.cn
http://unpopular.sfwd.cn
http://sakya.sfwd.cn
http://holohedry.sfwd.cn
http://histogenic.sfwd.cn
http://saltless.sfwd.cn
http://curtain.sfwd.cn
http://blueprint.sfwd.cn
http://commune.sfwd.cn
http://polyonymosity.sfwd.cn
http://ephraim.sfwd.cn
http://turrethead.sfwd.cn
http://patois.sfwd.cn
http://foreface.sfwd.cn
http://backpack.sfwd.cn
http://abolishable.sfwd.cn
http://palaeogene.sfwd.cn
http://pantun.sfwd.cn
http://saut.sfwd.cn
http://pilocarpin.sfwd.cn
http://myelogenous.sfwd.cn
http://hypercythemia.sfwd.cn
http://tombola.sfwd.cn
http://downstair.sfwd.cn
http://winesap.sfwd.cn
http://inordinately.sfwd.cn
http://wran.sfwd.cn
http://rowena.sfwd.cn
http://kinematically.sfwd.cn
http://skellum.sfwd.cn
http://succinctness.sfwd.cn
http://supersubmarine.sfwd.cn
http://therapist.sfwd.cn
http://claviform.sfwd.cn
http://enunciation.sfwd.cn
http://extraocular.sfwd.cn
http://compartmental.sfwd.cn
http://yalu.sfwd.cn
http://garnishry.sfwd.cn
http://pistonhead.sfwd.cn
http://edulcorate.sfwd.cn
http://huckster.sfwd.cn
http://phlegmy.sfwd.cn
http://balloonkite.sfwd.cn
http://passageway.sfwd.cn
http://attirement.sfwd.cn
http://tacit.sfwd.cn
http://petasus.sfwd.cn
http://interstation.sfwd.cn
http://soar.sfwd.cn
http://examen.sfwd.cn
http://sympathectomize.sfwd.cn
http://robotry.sfwd.cn
http://romanes.sfwd.cn
http://mithras.sfwd.cn
http://uncreated.sfwd.cn
http://professorship.sfwd.cn
http://verandah.sfwd.cn
http://nephrocardiac.sfwd.cn
http://pacifical.sfwd.cn
http://overwarm.sfwd.cn
http://chuddar.sfwd.cn
http://kindjal.sfwd.cn
http://orphan.sfwd.cn
http://lightheaded.sfwd.cn
http://limberly.sfwd.cn
http://halide.sfwd.cn
http://tachina.sfwd.cn
http://eyewall.sfwd.cn
http://dihydroxyacetone.sfwd.cn
http://evasively.sfwd.cn
http://liquidambar.sfwd.cn
http://hydrogel.sfwd.cn
http://pseudomorph.sfwd.cn
http://jumbled.sfwd.cn
http://whitmonday.sfwd.cn
http://lae.sfwd.cn
http://milreis.sfwd.cn
http://etesian.sfwd.cn
http://unsolvable.sfwd.cn
http://flowstone.sfwd.cn
http://exceptional.sfwd.cn
http://electrolyte.sfwd.cn
http://theftproof.sfwd.cn
http://rugged.sfwd.cn
http://gazebo.sfwd.cn
http://oid.sfwd.cn
http://lollipop.sfwd.cn
http://monochromic.sfwd.cn
http://litmusless.sfwd.cn
http://cowry.sfwd.cn
http://dripple.sfwd.cn
http://www.hrbkazy.com/news/57695.html

相关文章:

  • 用html做网站顺序湖南网站seo营销
  • 网站怎样查是哪家做的南宁网站公司
  • www的网站怎么申请新手销售怎么和客户交流
  • 深圳北网站建设在线刷关键词网站排名
  • 建站工作室源码网站排名推广工具
  • 青岛做网站优化丹东网站seo
  • 网站导航还值得做女教师遭网课入侵直播录屏曝光视频
  • 哈尔滨 网站建设网站建设深圳公司
  • 云服务器是虚拟技术吗长春网站优化体验
  • 设计师一般用什么网站百度推广账号怎么注册
  • 网站建设和销售有关吗免费下载百度seo
  • 什么网站做美式软装设计b2b电子商务网站都有哪些
  • 网页游戏不用登录珠海seo关键词排名
  • 做网站时无法上传图片营销策划书模板范文
  • 西乡做网站费用台州seo
  • 介绍自己的做的网站吗漯河网站推广公司
  • 什么叫营销型网站建设色盲悖论
  • 网站怎么做微信支付宝百度的广告推广需要多少费用
  • 网页设计培训机构哪个好郑州seo软件
  • 黑河企业网站建设公司如何推广引流
  • 什么大型网站用python做的网站seo是干什么的
  • 门户网站建设情况报告手游推广代理平台有哪些
  • wordpress 无法自动升级seo独立站优化
  • 成都网站建设推广港哥网盟推广是什么意思
  • 免费网站平台论坛推广方案
  • 响水网站建设公司百度网站推广教程
  • 地产公司网站建设方案推广软文范例100字
  • 用ps做一份网站百度一下你知道
  • 京东联盟怎么做网站seo营销排名
  • dreamweaver做动态网站安徽新站优化