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

舟山论坛网站建设黑龙江省建设集团有限公司网站首页

舟山论坛网站建设,黑龙江省建设集团有限公司网站首页,西安 网站建设外包,软件平台开发公司线程联合 当前线程邀请调用方法的线程优先执行#xff0c;在调用方法的线程执行结束之前#xff0c;当前线程不能再次执行。线程A在运行期间#xff0c;可以调用线程B的join()方法#xff0c;让线程B和线程A联合。这样#xff0c;线程A就必须等待线程B执行完毕后#xf…线程联合 当前线程邀请调用方法的线程优先执行在调用方法的线程执行结束之前当前线程不能再次执行。线程A在运行期间可以调用线程B的join()方法让线程B和线程A联合。这样线程A就必须等待线程B执行完毕后才能继续执行。 join方法的使用 join()方法就是指调用该方法的线程在执行完run()方法后再执行join方法后面的代码即将两个线程合并用于实现同步控制。 class A implements Runnable{private Thread b;public A(Thread b){this.b b;}Overridepublic void run() {for(int i0;i10;i){System.out.println(Thread.currentThread().getName() A i);if(i 5){try {this.b.join();} catch (InterruptedException e) {e.printStackTrace();}}try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}} }class B implements Runnable{Overridepublic void run() {for(int i0;i20;i){System.out.println(Thread.currentThread().getName() B i);try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}} }public class TestJoinThread {public static void main(String[] args) {Thread t1 new Thread(new B());Thread t new Thread(new A(t1));t.start();t1.start();for(int i0;i10;i){System.out.println(Thread.currentThread().getName() i);if(i 2){try {t.join();} catch (InterruptedException e) {e.printStackTrace();}}try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}} } 线程联合案例 需求 实现爸爸让儿子买烟。 /*** 儿子买烟线程*/ class SonThread implements Runnable{Overridepublic void run() {System.out.println(儿子出门买烟);System.out.println(儿子买烟需要10分钟);for(int i0;i10;i){System.out.println(第i分钟);try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}System.out.println(儿子买烟回来了);} }/*** 爸爸抽烟线程*/ class FatherThread implements Runnable{Overridepublic void run() {System.out.println(爸爸想抽烟发现烟抽完了);System.out.println(爸爸让儿子去买一包红塔山);Thread t new Thread(new SonThread());t.start();System.out.println(等待儿子买烟回来);try {t.join();} catch (InterruptedException e) {e.printStackTrace();System.out.println(爸爸出门找儿子);System.exit(1);}System.out.println(爸爸高兴的接过烟并把零钱给了儿子);} }public class TestJoinDemo {public static void main(String[] args) {System.out.println(爸爸和儿子买烟的故事);Thread t new Thread(new FatherThread());t.start();} } Thread类中的其他常用方法 获取线程名称getName() 方式一 this.getName()获取线程名称该方法适用于继承Thread实现多线程方式。 class GetName1 extends Thread{Overridepublic void run() {System.out.println(this.getName());} } 方式二 Thread.currentThread().getName()获取线程名称该方法适用于实现Runnable接口实现多线程方式。 class GetName2 implements Runnable{Overridepublic void run() {System.out.println(Thread.currentThread().getName());} } 设置线程的名称setName() 方式一 通过构造方法设置线程名称。 class SetName1 extends Thread{public SetName1(String name){super(name);}Overridepublic void run() {System.out.println(this.getName());} }public class SetNameThread {public static void main(String[] args) {SetName1 setName1 new SetName1(SetName1);setName1.start();} } 方式二 通过setName()方法设置线程名称。 class SetName2 implements Runnable{Overridepublic void run() {System.out.println(Thread.currentThread().getName());} } public class SetNameThread {public static void main(String[] args) {Thread thread new Thread(new SetName2());thread.setName(SetName2);thread.start();} } 判断线程是否存活isAlive() isAlive()方法 判断当前的线程是否处于活动状态。 活动状态是指线程已经启动且尚未终止线程处于正在运行或准备开始运行的状态就认为线程是存活的。 class Alive implements Runnable{Overridepublic void run() {for(int i0;i4;i){System.out.println(Thread.currentThread().getName() i);try {Thread.sleep(500);} catch (InterruptedException e) {e.printStackTrace();}}} }public class TestAliveThread {public static void main(String[] args) {Thread thread new Thread(new Alive());thread.setName(Alive);thread.start();System.out.println(thread.getName() thread.isAlive());try {Thread.sleep(4000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(thread.getName() thread.isAlive());} }
http://www.hkea.cn/news/14411251/

相关文章:

  • 网站建设主要工作内容wordpress禁止百度抓取
  • 石家庄网站建设哪家好word模板网站
  • 统计网站访客人数江西省赣州市信丰县
  • 怎么查网站的备案传奇广告查询网站
  • 化工建网站多少费用旅游网站首页图片
  • 大连模板做网站常熟住房和城乡建设局网站
  • 手机什么app做网站网络设计过程
  • 朋友圈海报用什么网站做的wordpress发布的文章404
  • 高端网站案例网站建设开发高端市场
  • 微信手机网站muse怎么做响应式网站
  • 安徽省网站肥建设网站外贸公司怎么接订单
  • 如何建设平台型的网站沈阳关键词seo
  • 免费网站制作知乎深圳快速网站制
  • 网站魔板大全产品做优化好还是超级网站好
  • 商城网站建设快速服务杭州余杭网站建设
  • 网站前端做出来后台怎么做优秀设计作品的网站
  • 莱芜网站建设方案公司招聘58同城找工作
  • 微信用网站怎么做网站常用的一种js的图片幻灯片特效代码
  • 深圳网站制作开发网页设计与网站建设教材
  • 哪家网站专门做折扣销售免费公章在线生成器
  • 重庆市建设工程造价管理总网站网站页面描述
  • 关于做网站的论文武威网站建设
  • 海口网站建设q.479185700惠广州最新消息
  • 简述织梦网站上传步骤部署wordpress
  • 做百度移动端网站排名软件wordpress镜像什么意思
  • 如何制作论坛网站洛阳最好的做网站的公司
  • 做外汇需要了解的网站如何进行网站推广?网站推广的基本手段有哪些
  • 网络营销类网站优化好的网站做企业网站
  • 简约个人网站微信是什么公司开发的
  • 罗湖做网站qq空间网站根目录