(JAVA)多线程之wait()与notify()

/*** @author Alina* @date 2021年12月22日 12:17 上午**/
class Student{String name;String sex;boolean flag = false;}
class Product implements Runnable{Student s ;Product(Student s ){this.s = s;}public void run(){int x = 0;//通过奇数偶数进行赋值while(true){synchronized (s){if(s.flag){try {s.wait();}catch (Exception e){}}if (x%2 == 1){s.name = "张三";s.sex = "男";}else {s.name = "李四";s.sex = "女";}x++;s.flag = true;s.notify();}}}
}
class Custom implements Runnable{Student s;Custom(Student s ){this.s = s;}public void run(){synchronized (s){while (true){if(!s.flag){try{s.wait();}catch (Exception e){}}System.out.println(s.name+"..."+s.sex);s.flag = false;s.notify();}}}}
public class SourceThreadDemo {public static void main(String[] args) {Student s = new Student();new Thread(new Product(s)).start();new Thread(new Custom(s)).start();}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/421707.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ASP.NET中数据有效性校验的方法

主要用Regex的IsMatch方法,在BusinessRule层进行校验数据的有效性,并将校验的方法作为BusinessRule层基类的一部分。 在WebUI层现实提示信息。 using System;using System.Data;using System.Text.RegularExPRessions;namespace Education.BusinessRules…

(Java) 线程池

线程池:1.出现版本:JDK1.52.包:java.util.concurrent3.Executors类 -->工厂类1.三个静态方法:static ExecutorService newCachedThreadPool() 创建新的线程池对象Creates a thread pool that creates new threads as needed, …

openSUSE 13.1 Milestone 2 发布

openSUSE 13.1 发布第二个里程碑版本,下载地址: openSUSE-Factory-KDE-Live-Build0504-x86_64.iso (917MB, MD5, torrent)openSUSE-Factory-GNOME-Live-Build0504-x86_64.iso (889MB, MD5, torrent).更新的软件包括: Automake 1.13.2, Evolut…

UDP发字节,接收字节

public class MenuBarDemo {public static void main(String[] args) throws Exception{byte[] bytes "你好".getBytes(StandardCharsets.UTF_8);DatagramPacket dp new DatagramPacket(bytes,bytes.length, InetAddress.getByName("127.0.0.1"),10000);…

c语言把int指针强制转换成void指针_神一般的C语言指针,你看懂多少?

下面,做个测试,如果下面的指针问题你能够全都解答正确,那么你的C语言功底真是神一般的了。http://1.int *p;http://2.int **p;http://3.int *p[10];http://4.int (*p)[10];http://5.int *p(int);http://6.int (*p)(int);http://7.int (*p[10]…

基于混合云存储系统的电影推荐引擎小结

基于混合云存储系统的电影推荐引擎 推荐算法部分是Mahout下的Taste实现的, 数据集采用GroupLens 的数据集合,将这些数据集转换到mysql数据库中 其中Taste:http://mahout.apache.org/ GroupLens数据集:http://www.grouplens.org/n…

懒汉式高并发实例

/*** author Alina* date 2021年12月26日 1:48 上午*/ public class SingleLanhan {private SingleLanhan() { }private static SingleLanhan s null;public static SingleLanhan getInstance(){if (s null){synchronized (SingleLanhan.class){if (s null){s new SingleL…

机器学习里面的基函数_厉害了!机器学习用到SSD里面

近年来人工智能和大数据的迅速发展正在深刻改变着这个世界和我们的生活方式。人工智能的核心是机器学习(Machine Learning) 算法,自 2006 年以来,在机器学习领域,以深度学习(Deep Lerning) 为代表的机器学习算法取得了突破性的进展&#xff0…

【好用的ORM框架】

对于开发采用orm会带来很大便利,orm即是数据表和实体对于关系框架。内部封装增删改查。它不仅适用于mvc,而且在winform和webform都适用。下面具体介绍orm是使用: 初步在winform为例使用,随后在在mvc大量运用: 1&#x…

愤怒的小鸟4只编外鸟_加西亚时隔三年夺美巡第11冠,马尔纳蒂9鸟单独第二

桑德森农场锦标赛战罢,第三轮并列领先的西班牙名将加西亚决赛轮打出5鸟1鹰2柏忌,以-19(68-68-66-67)的总成绩1杆优势夺冠,结束了三年的冠军荒。后来居上的马尔纳蒂决赛轮狂抓9鸟没有吞下一个柏忌,收获了职业生涯最低杆63杆&#x…

(JAVA)反射

import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method;/*** author Alina* date 2021年12月30日 10:47 下午* JAVA 反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法&#…

带分数 - 蓝桥杯

标题:带分数 - 蓝桥杯 内容:2013年蓝桥杯全国软件大赛预赛,第9题。补充了memset()和memcpy()两个函数,在数组赋值中常用到。 作者:MilkCu 题目描述 标题:带分数 100 可以表示为带分数的形式:100…

(JAVA)reflect练习

className Student methodName student import java.awt.*; import java.io.FileReader; import java.lang.reflect.Method; import java.util.Properties;/*** author Alina* date 2022年01月08日 7:08 下午*/ public class ReflectPra {public static void main(String[] …

pgslq表的字段类型,如何在PostgreSQL中获取表的列表列名称和数据类型?

With the following query, we can get a list of column names and datatype of a table in PostgreSQL.解决方案SELECTa.attname as "Column",pg_catalog.format_type(a.atttypid, a.atttypmod) as "Datatype"FROMpg_catalog.pg_attribute aWHEREa.attnu…

【Linux操作系统分析】设备驱动处理流程

1 驱动程序,操作系统,文件系统和应用程序之间的关系 字符设备和块设备映射到操作系统中的文件系统,由文件系统向上提供给应用程序统一的接口用以访问设备。 Linux把设备视为文件,称为设备文件,通过对设备文件的读写操作…

C语言基础之数据结构

1.32 换成数字23 # include<stdio.h> //编译预处理指令 # include <math.h> # define pi 3.1415926 //定义不变常量 int convert(int num); char ABC(char a); double sum(double a,double b, double c); double area(double a,double b, double c); double num(…