notifyAll()_notify for

notifyAll()_notify for下面这段代码suclassCalculaterextendsThread{ publicinttotal=1; publicvoidrun(){ System.out.println(“Calculatorrun”); synchronized(this){ for(inti=1;i<10;i++){ total*=i; }

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

下面这段代码说明了, notifyAll将所有等待在Calculater对象cal的对象锁上的所有线程从阻塞状态变为可运行状态, 但是他们要获得cal对象锁才能继续执行, 随意他们会按随机顺序一个一个的执行完剩下的代码。

class Calculater extends Thread{
	public int total = 1;
	public void run(){
		System.out.println("Calculator run");
		synchronized(this){
			for(int i=1; i<10; i++){
				total *= i;
			}
			notifyAll();
		}
	}
}

public class NotifyAllTest extends Thread{
	public Calculater cal;
	public NotifyAllTest(Calculater cal){
		this.cal = cal;
	}
	public void run(){
		System.out.println(this.currentThread()+" runing ");
		synchronized(this.cal){
			try {
				this.cal.wait();
				System.out.println(this.currentThread() + "wake up");
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println(this.currentThread()+" : total = "+this.cal.total);
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	public static void main(String [] args) throws InterruptedException{
		Calculater cal = new Calculater();
		new NotifyAllTest(cal).start();
		new NotifyAllTest(cal).start();
		new NotifyAllTest(cal).start();
		new NotifyAllTest(cal).start();
		new NotifyAllTest(cal).start();
		Thread.sleep(1000);
		cal.start();
	}

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/197761.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • mysql 左连接 自连接 例子

    mysql 左连接 自连接 例子连接就是将两个表按照某个公共字段来拼成一个大表。左连接就是在做连接是以左边这个表为标准,来遍历右边的表。例子:用户访问记录:问题:查出看了湖南卫视但没有看北京卫视的用户信息逻辑:先通过左连接将看了湖南卫视和北京卫视的查出来,然后再将看了湖南卫视但不在刚才查出的结果中的用户查出来。SELECT*FROMtest_visitWHEREchannel=’

    2022年5月28日
  • redis 乐观锁_jpa乐观锁

    redis 乐观锁_jpa乐观锁文章目录GeospatialHyperloglogBitmapsRedis事务悲观锁和乐观锁JedisSpringboot继承RedisGeospatial存储地理位置的数据结构应用场景朋友的定位,附近的人,打车距离计算Geospatial底层使用的是Zset127.0.0.1:6379> geoadd city 116.23 40.22 beijing 添加一个数据127.0.0.1:6379> geoadd city 121.47 31.23 shanghai 118.77

    2022年8月8日
  • CodeForces 377B—Preparing for the Contest(二分+贪心)

    CodeForces 377B—Preparing for the Contest(二分+贪心)

    2021年12月10日
  • 如何用python画一个心形图_python画心形名字

    如何用python画一个心形图_python画心形名字Python可以使用turtle库来画爱心。Turtle库是Python语言中一个很流行的绘制图像的函数库,想象一个小乌龟,在一个横轴为x、纵轴为y的坐标系原点,(0,0)位置开始,它根据一组函数指令的控制,在这个平面坐标系中移动,从而在它爬行的路径上绘制了图形。实现代码如下:fromturtleimport*pensize(1)pencolor(‘red’)fillcolor(‘pink…

    2022年9月7日
  • 如何黑钓鱼网站_初学钓鱼视频教程全集

    如何黑钓鱼网站_初学钓鱼视频教程全集一、被盗号找上门今天一打开QQ邮箱,看到个群邮件这货以发聚会照片为名义给出了一个网址Defaulthttp://202.194.131.20/xcb/xcb/link.php?id=73&url=http://yyhhj.emy.in/cc/?9330461http://202.194.131.20/xcb/xcb/link.php?id=73&url=http://yyhhj.e…

    2022年8月24日
  • Docker安装Nexus3私服以及使用

    Docker安装Nexus3私服以及使用一、Nexus简介二、安装步骤(使用docker)1、搜索Nexus3镜像dockersearchnexus​2、拉取Nexus3镜像dockerpullsonatype/nexus3​3、创建Nexus3容器dockerrun-itd-p8081:8081–privileged=true–namenexus3\-v/data/nexus-data:/var/nexus-data–restar…

    2022年7月13日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号