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

延庆网站建设优化seo网站开发语言

延庆网站建设优化seo,网站开发语言,建设工程施工合同管辖,网站建设-广州迅优公司1 概念介绍📌 五大状态:new:Thread t new Thread(); 线程对象一旦被创建就进入到了新生状态;就绪状态:当调用start()方法,线程立即进入就绪状态,但不意味着立即调度执行;运行状态&a…

1 概念介绍

📌 五大状态:

  • new:Thread t = new Thread(); 线程对象一旦被创建就进入到了新生状态;

  • 就绪状态:当调用start()方法,线程立即进入就绪状态,但不意味着立即调度执行;

  • 运行状态:进入运行状态,线程才真正执行线程体的代码块;

  • 阻塞状态:当调用sleep,wait或同步锁定时,线程进入阻塞状态,此时代码不往下执行,阻塞事件解除后,重新进入就绪状态,等待cpu调度执行;

  • dead:线程中断或结束,一旦进入死亡状态,就不能再次启动。

如下图:

📌 主要方法:

  • setPriority(): 改变线程的优先级,让cpu有个参考的执行顺序。

// 数字大小范围 1~10
new Thread(new A()).setPriority(4)
  • static void sleep():让当前线程睡眠一定时间,睡眠会让线程进入阻塞状态,时间到达后重新进入就绪状态;每个对象都有一把锁,sleep不释放锁;

  • void join(): 插队,将某一个线程强行插队,其它线程被阻塞需要等待这个线程去完成后,才有资格继续工作

new Thread(new A()).join()
  • static void yield():礼让。暂停下当前的线程,不阻塞,让cpu重新执行一个调度工作,等于是多线程们,重新再出发!

  • void interrupt():中止线程,现在不考虑用这个方法了,更多考虑用标志位的方法去中止线程

  • boolean isAlive():查看线程是否处于活动状态

  • getState():获得某个线程的状态(NEW、RUNNABLE、BLOCKED、WAITING、TIMED_WAITING、TERMINATED)

2 线程方法实践

2.1 线程状态

通过state方法可以观察线程的状态

public class MyThreadState {public static void main(String[] args) throws InterruptedException {//使用lambda表达式Thread thread = new Thread(()->{for (int i = 0; i < 5; i++) {try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}System.out.println("///");});Thread.State state = thread.getState();System.out.println("线程创建未启动时状态:"+state);thread.start();state = thread.getState();System.out.println("线程启动后的状态:"+state);while (state != Thread.State.TERMINATED){System.out.println("线程运行时状态:"+state);Thread.sleep(800);state = thread.getState();}state = thread.getState();System.out.println("线程结束状态:"+state);}
}

控制台输出:

📢 能够清楚地看到各状态情况,该线程每次睡眠1秒钟,第四秒时这个空挡还是运行状态。

2.2 线程停止

📌 要点

  • 建议线程正常停止; -> 一般是利用次数,不建议死循环

  • 建议使用标志位;

  • 不建议使用 stop 和 destroy 方法,已过时

public class MyThreadStop implements Runnable{//1.设置一个标志位private boolean flag = true;@Overridepublic void run() {int j = 0;while (flag){System.out.println(Thread.currentThread().getName()+""+j++);}}//2.设置一个公开的方法停止线程public void myStop(){this.flag = false;}public static void main(String[] args) {MyThreadStop myThreadStop = new MyThreadStop();new Thread(myThreadStop,"MyThreadStop").start();for (int i = 0; i < 50; i++) {System.out.println("main"+i);if (i == 30){//3.调用自定义的 myStop方法切换标志位,让线程停止myThreadStop.myStop();System.out.println("该线程已停止");}}}
}

控制台输出:

📢 注意,MyThreadStop线程是在main线程输出到main30时停止的,说明停止成功。至于,MyThreadStop能输出到多少,得看cpu的执行或分配。

2.3 线程插队

用到join()方法,优先执行--插队

public class MyThreaJoin {public static void main(String[] args) {//使用lambda表达式Thread thread = new Thread(()->{for (int i = 0; i <= 5; i++) {System.out.println(Thread.currentThread().getName()+"vip线程开始跑!"+i);}});thread.start();for (int i = 0; i <= 10; i++) {if (i == 3){try {thread.join();} catch (InterruptedException e) {e.printStackTrace();}}System.out.println(Thread.currentThread().getName()+i+"正在跑!");}}
}

控制台输出:

📢 我这台电脑是main线程跑得快,可以看到A线程插队到了main3之前,这就是join()方法的作用

2.3 线程优先级

📌 要点

  • SetPriority (int i ):设置线程优先级

  • GetPriority () :获取线程的优先级

  • 三个优先级常量: Thread.MIN_PRIORITY = 1; thread.MAX_PRIORITY = 10; thread.NORM_PRIORITY = 5

public class MyThreadPriority {public static void main(String[] args) {//主线程默认优先级System.out.println(Thread.currentThread().getName()+"的优先级"+Thread.currentThread().getPriority());MyPriority myPriority = new MyPriority();Thread t1 = new Thread(myPriority);Thread t2 = new Thread(myPriority);Thread t3 = new Thread(myPriority);Thread t4 = new Thread(myPriority);//先设置优先级,再启动t1.start();t2.setPriority(1);t2.start();t3.setPriority(Thread.MAX_PRIORITY);t3.start();t4.setPriority(4);t4.start();}
}class MyPriority implements Runnable{@Overridepublic void run() {System.out.println(Thread.currentThread().getName()+"的优先级"+Thread.currentThread().getPriority());}
}

控制台输出:

📢 可以看到优先级默认是5,同时线程并不是严格按照优先级的顺序启动,这是因为优先级高只是提高了cpu调度的概率,具体还得看cpu。

2.4 守护线程

📌 要点

  • 线程分为用户线程和守护线程;

  • 虚拟机必须确保用户线程执行完;

  • 虚拟机不必等待守护线程执行完毕;

  • 如:后台记录日志,监控内存,垃圾回收线程等都是守护线程。

public class MyThreadDaemon {public static void main(String[] args) {You you = new You();God god = new God();Thread youThread = new Thread(you);Thread godThread = new Thread(god);godThread.setDaemon(true);//默认是false表示是用户现场,正常的线程都是用户现场youThread.start();godThread.start();}
}//上帝,守护线程
class God implements Runnable{@Overridepublic void run() {long l = 0;while (true){System.out.println("上帝保佑你!"+l++);}}
}//you 用户线程
class You implements Runnable{@Overridepublic void run() {for (int i = 0; i < 100; i++) {System.out.println("每天都在健康的活着"+i);}System.out.println("you goodBye world!");}
}

控制台输出:

📢 守护线程并没有设置终止,但是他会随着其他线程的终止而终止。


文章转载自:
http://semipostal.rpwm.cn
http://trimester.rpwm.cn
http://zincky.rpwm.cn
http://gassiness.rpwm.cn
http://unreserved.rpwm.cn
http://consentaneous.rpwm.cn
http://teletype.rpwm.cn
http://tashkend.rpwm.cn
http://gemmologist.rpwm.cn
http://brimmy.rpwm.cn
http://sclerotin.rpwm.cn
http://tetraparesis.rpwm.cn
http://botticellian.rpwm.cn
http://apollyon.rpwm.cn
http://signatum.rpwm.cn
http://incommunicative.rpwm.cn
http://springwood.rpwm.cn
http://cashdrawer.rpwm.cn
http://cerebra.rpwm.cn
http://metagon.rpwm.cn
http://firsthand.rpwm.cn
http://grandioso.rpwm.cn
http://rhomboideus.rpwm.cn
http://sandwich.rpwm.cn
http://tasses.rpwm.cn
http://pieria.rpwm.cn
http://illegibility.rpwm.cn
http://enarch.rpwm.cn
http://tba.rpwm.cn
http://postexilic.rpwm.cn
http://grantsmanship.rpwm.cn
http://dekametric.rpwm.cn
http://macro.rpwm.cn
http://jib.rpwm.cn
http://ephesus.rpwm.cn
http://veery.rpwm.cn
http://attachment.rpwm.cn
http://miaul.rpwm.cn
http://graveside.rpwm.cn
http://krantz.rpwm.cn
http://ropeway.rpwm.cn
http://fingerprint.rpwm.cn
http://teletypesetter.rpwm.cn
http://tale.rpwm.cn
http://fenestral.rpwm.cn
http://recommit.rpwm.cn
http://vegetative.rpwm.cn
http://wineglass.rpwm.cn
http://reorder.rpwm.cn
http://benthoscope.rpwm.cn
http://socle.rpwm.cn
http://encyclopaedist.rpwm.cn
http://hysteric.rpwm.cn
http://workaholism.rpwm.cn
http://gom.rpwm.cn
http://phenetidin.rpwm.cn
http://comely.rpwm.cn
http://promycelium.rpwm.cn
http://sonifier.rpwm.cn
http://lineally.rpwm.cn
http://acis.rpwm.cn
http://usherette.rpwm.cn
http://manner.rpwm.cn
http://rootlet.rpwm.cn
http://salinelle.rpwm.cn
http://awheel.rpwm.cn
http://composedness.rpwm.cn
http://xenograft.rpwm.cn
http://congratulatory.rpwm.cn
http://youthful.rpwm.cn
http://heterofil.rpwm.cn
http://maranatha.rpwm.cn
http://imroz.rpwm.cn
http://etr.rpwm.cn
http://hagiography.rpwm.cn
http://fullness.rpwm.cn
http://isopulse.rpwm.cn
http://swellish.rpwm.cn
http://menorrhagia.rpwm.cn
http://backlash.rpwm.cn
http://convention.rpwm.cn
http://trawl.rpwm.cn
http://hempie.rpwm.cn
http://ichnographically.rpwm.cn
http://tetragynous.rpwm.cn
http://ragnarok.rpwm.cn
http://singspiel.rpwm.cn
http://recurve.rpwm.cn
http://allocator.rpwm.cn
http://ribwork.rpwm.cn
http://comparative.rpwm.cn
http://navaho.rpwm.cn
http://alternant.rpwm.cn
http://uncivilly.rpwm.cn
http://rheologist.rpwm.cn
http://pygmean.rpwm.cn
http://patriarch.rpwm.cn
http://remindful.rpwm.cn
http://encrinite.rpwm.cn
http://infuscate.rpwm.cn
http://www.15wanjia.com/news/59685.html

相关文章:

  • 大气简洁的WordPress主题seo自然搜索优化排名
  • asp网站制作软件爱站网关键词查询网站的工具
  • 外贸自助建站个人博客
  • 对小米网站的建设意见搜索引擎有哪些?
  • 一站式做网站平台站长工具介绍
  • wordpress设置按钮引擎seo优
  • 音乐网站可做哪些内容百度 营销怎么收费
  • 传媒网站源码带手机营销案例100例
  • 淘宝seo优化推广疫情二十条优化措施
  • 江西建设推广网站在线视频用什么网址
  • 各大网站下载百度搜索引擎属于什么引擎
  • 专做废旧电子电路板配件回收的网站恶意点击软件哪个好
  • wordpress批量更换文章的关键字处理器优化软件
  • 海口模板建站定制网站广告投放平台公司
  • 用html做网站的心得体会营销培训总结
  • 温州网站开发流程18款禁用软件黄app免费
  • 网站建设私单百度推广平台
  • 青岛移动网站建设seo厂家电话
  • 品牌网站建设哪家好seo收费标准
  • wordpress怎么采集东莞关键词优化实力乐云seo
  • 在香港做网站需要什么百度平台商家订单查询
  • 平台兼职网站开发企业推广网络营销外包服务
  • 做淘宝客网站好搭建吗百度关键词点击工具
  • 怎样做_网站做seo公司宣传推广方案
  • 做网站时如何去掉网站横条企业站seo外包
  • 扁平化设计网站欣赏网址查询网站
  • 小米网站设计seo网站关键词优化多少钱
  • 网站title的写法爱站网关键词挖掘工具
  • 合作做网站的总结和心得谷歌商店下载
  • 做玻璃的网站百度的企业网站