资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Java线程通信源代码分析

本篇内容介绍了“Java线程通信源代码分析”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

网站建设哪家好,找创新互联建站!专注于网页设计、网站建设、微信开发、微信小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了南关免费建站欢迎大家使用!

其实我们在源代码中就能发现其中的奥秘。因为ThreadNum和ThreadChar都有对Objecto的引用,所以你wait和notify的时候都应该同步,Java线程通信具体看如下:

public class Test8 {  public static void main(String[] args){   Object o=new Object();   Thread n=new ThreadNum(o);   Thread c=new ThreadChar(o);   n.start();   c.start();   }   }   class ThreadNum extends Thread{   Object o;   public ThreadNum(Object o){   this.o=o;   }   public void run(){   for(int i=1;i<26;i++){   System.out.println(i);   System.out.println(++i);   try {   synchronized (this) {  this.wait();   }  } catch (InterruptedException e) {}   synchronized (this) {   this.notify();   }  }   }   }   class ThreadChar extends Thread{   Object o;   public ThreadChar(Object o){   this.o=o;   }   public void run(){   for(char a='A';a<='Z';a++){   System.out.println(a);   synchronized (this) {   this.notify();   }  try {   synchronized (this) {  this.wait();   }  } catch (InterruptedException e) {}   }   }   }

“Java线程通信源代码分析”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!


新闻名称:Java线程通信源代码分析
本文网址:http://cdkjz.cn/article/ppgihd.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220