- 浏览: 192316 次
文章分类
最新评论
wait 和 notify 是Object类而非Thread类的两个方法,只有在线程同步的时候才有效,只出现在synchronize方法或块中。
wait(0) 无限等待
notify()
import java.util.*; public class JwaitTest { static List<String> pic = new ArrayList<String>(); static boolean done = false; static class Download extends Thread{ private String[] image = {"kobe.jpg","tmac.jpg"}; @Override public void run() { for(int i=0;i<image.length;i++){ int time = new Random().nextInt(100)*10; System.out.println("下载图片:"+time); try { Thread.sleep(time); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } synchronized(pic){ System.out.println("Download" + image[i]); pic.add(image[i]); pic.notify(); } } done = true; System.out.println("Download thread exit!"); } } static class Display extends Thread{ @Override public void run() { while(!done){ synchronized(pic){ try { pic.wait(200); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(pic.size()>0){ System.out.println("Display\t"+pic.remove(0)); } } } System.out.println("Display thread exit!"); } } /** * @param args */ public static void main(String[] args) { new Download().start(); new Display().start(); } } public class JwaitTest { static List<String> pic = new ArrayList<String>(); static boolean done = false; static class Download extends Thread{ private String[] image = {"kobe.jpg","tmac.jpg"}; @Override public void run() { for(int i=0;i<image.length;i++){ int time = new Random().nextInt(100)*10; System.out.println("下载图片:"+time); try { Thread.sleep(time); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } synchronized(pic){ System.out.println("Download" + image[i]); pic.add(image[i]); pic.notify(); } } done = true; System.out.println("Download thread exit!"); } } static class Display extends Thread{ @Override public void run() { while(!done){ synchronized(pic){ try { pic.wait(200); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(pic.size()>0){ System.out.println("Display\t"+pic.remove(0)); } } } System.out.println("Display thread exit!"); } } /** * @param args */ public static void main(String[] args) { new Download().start(); new Display().start(); } }
多个线程挑选一个
notifyAll()
发表评论
-
JDK配置
2012-09-19 14:13 677JAVA_HOME C:\Program Files\ ... -
Java abstract class interface 区别
2012-09-09 23:35 9781.abstract class 表示的是一种继承关系,并 ... -
全组合
2012-08-28 23:10 866package cn.xmu.microsoft; ... -
全排列
2012-08-28 11:35 829private static String[] perm ... -
Java 方法引用
2012-08-12 11:23 756一个对象的引用本质上是一个很强的完整和安全方面约束能力的对象指 ... -
Java 动态绑定 静态绑定
2012-08-12 11:07 802静态绑定:使用编译类型的对象引用 动态绑定:使用运行类型所指 ... -
A Java Runtime Environment (JRE) or Java Development Kit (JDK) must be available
2012-05-26 15:34 939Eclipse 在 Unbutu下双击图标启动会出现上述问题, ... -
Java BitSet
2012-04-13 09:29 916//说明 bs 至少能够装下65bits 大小由系统 ... -
Java 中文编码问题
2012-04-10 22:09 763import java.io.UnsupportedEn ... -
Hadoop in-mapper combining 实例
2012-04-08 14:46 2591import java.io.IOException; ... -
Java 正则表达式
2012-04-08 09:50 850Pattern p = Pattern.compile( ... -
Java 加载属性配置文件
2012-04-07 16:49 670import java.util.MissingReso ... -
Dangling meta character '*' near index 0
2012-04-05 14:12 1207这个由于是在java中使用split(“”);造成的,在 ... -
Logger
2012-04-05 08:54 775/LogTest/src/log4j.properties ... -
分割字符串时存在多个分隔符
2012-03-20 17:14 899public static void main(Str ... -
Error: could not open `C:Program FilesJavajre6libamd64jvm.cfg'
2012-02-26 18:55 1069重新配置环境变量后出现可以编译,不可以运行... 删除 ... -
JAVA 运行包中的类
2012-02-23 13:41 1068Java中的包是以目录的形式组织的,在java文件中如果指定了 ... -
Data Access Object
2012-02-18 21:24 746DAO:Data Access Object 把项目开发分为 ... -
文件读写问题
2012-02-16 13:36 734'赵','1234' '钱','2345''孙','3456 ... -
JUnit hamcrest匹配器
2012-02-16 12:04 932Hamcrest带有一个有用的匹配器库.以下是一些最重要的. ...
相关推荐
那些年,与你同分同位次的同学都去了哪里?全国各大学在辽宁2020-2024年各专业最低录取分数及录取位次数据,高考志愿必备参考数据
下单系统的Spnigboot和微信小程序实现(全栈微信小程式下单)
该项目是一款基于Java的智能文件管家设计源码,涵盖102个文件,包括29个Java源文件、27个类文件、19个XML配置文件、10个YAML文件、8个列表文件、4个属性文件、4个JAR包文件以及1个Git忽略文件。该系统旨在提供高效便捷的文件管理解决方案。
基于YoloV8的简单目标检测和跟踪,使用KMNET进行鼠标移动(处理多目标移动抖动,处理鼠标平滑移动)
本项目是一款基于Vue和JavaScript开发的心旅途个性化推荐旅游平台设计源码,整合了513个Java文件、76个PNG图片、70个XML配置文件、62个JavaScript文件、42个Vue组件文件、28个CSS样式文件、22个HTML文件、18个YAML配置文件、16个属性文件、11个Vue模板文件,总计919个文件。平台采用现代化前端技术堆栈,旨在为用户提供个性化的旅游推荐服务。
AutoLine是一个基于Python的通用自动化测试开源平台,包含了657个文件,涵盖228个PNG图片、209个CSS样式、95个JavaScript脚本、39个Python源代码、21个HTML文件、19个XML文件、14个GIF图片、6个DS_Store文件、5个文本文件、4个Markdown文件。该平台的设计源码由多种编程语言编写,旨在提供灵活高效的自动化测试解决方案。
微信小程序图像裁剪工具_ e-cropper
【作品名称】:基于MATLAB的答题卡识别系统。带一个GUI可视化界面,通过输入答题卡旋转校正,边缘检测,霍夫曼变换检测答题卡填涂区域 【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。 【项目介绍】: 传统的阅卷方式为流水线的手工作业方式。这样的方式存在很多的问题,因为很容易受到阅卷者的主观因素的影响,从而产生一定的偏差。所以很多人就讨论如何将人为的因素降低到最低,来确保考生的考试成绩的公平公正和准确。 基于MATLAB的答题卡识别系统。带一个GUI可视化界面,通过输入答题卡旋转校正,边缘检测,霍夫曼变换检测答题卡填涂区域,分割,识别属于ABCD等,通过和实现设置好的标准答案excel对比,从而得出最终分数 【资源声明】:本资源作为“参考资料”而不是“定制需求”,代码只能作为参考,不能完全复制照搬。需要有一定的基础能够看懂代码,能够自行调试代码并解决报错,能够自行添加功能修改代码。
那些年,与你同分同位次的同学都去了哪里?全国各大学在辽宁2020-2024年各专业最低录取分数及录取位次数据,高考志愿必备参考数据
本项目深入解析并实现了基于Java核心技术的Nacos配置中心,包含2707个文件,涵盖2180个Java源文件、177个JavaScript文件、52个XML文件、35个SCSS文件、22个PEM文件、20个属性文件、18个Markdown文件、16个Protocol Buffers文件、12个JSON文件、11个字体文件。项目涉及多种语言和技术,旨在提供一个全面的配置中心解决方案。
枝晶生长Comsol仿真模型。 锂枝晶生长过程的 枝晶生长Comsol仿真模型。 锂枝晶生长过程的枝晶形貌,温度场耦合,应力场,浓度场,电势场。 C++程序,基于元胞自动机法模拟枝晶生长,能实现任意角度(偏心正方算法),同时采用LBM考虑了对流作用对枝晶生长的影响
本项目为apple_pro客户关系管理系统的组件化开发源码,采用Python、CSS、HTML和JavaScript等多种语言编写,总计包含1078个文件。其中,Python源文件254个,Python编译后文件244个,CSS样式文件65个,HTML模板61个,JavaScript脚本40个,以及其他类型文件如LESS、SCSS、XML、PNG等。该系统通过组件化设计,旨在提升客户关系管理的效率与用户体验。
微信小程序日历插件_Calendar
那些年,与你同分同位次的同学都去了哪里?全国各大学在辽宁2020-2024年各专业最低录取分数及录取位次数据,高考志愿必备参考数据
另一个小型购物中心。Litemall=Spring Boot后端+Vue管理员前端+微信小程序用户前端+Vue用户移动端_stemall
该项目为基于GitHub的ESPnet语音处理工具包设计源码,包含10633个文件,涵盖Shell脚本、Python、MATLAB、C++等多种编程语言。文件类型包括2872个shell脚本、2303个YAML配置文件、1662个Python脚本、1567个配置文件、306个Markdown文件、223个Perl脚本、39个文本文件、35个Bash脚本、27个PNG图片、21个补丁文件。该项目定期更新,适用于语音处理领域的研究与开发。
该项目是一款基于Python开发的pyecharts可视化图表库源码,包含166个文件,涵盖了121个Python源文件、12个HTML文件、9个JSON文件、6个PNG图片文件、4个Markdown文件、3个文本文件、2个YAML文件以及少量配置和管理文件。该库旨在提供强大的数据可视化解决方案,适用于各种数据分析与展示需求。
STM32软件学习资料GPS与GPRSSTM32软件学习资料GPS与GPRS
那些年,与你同分同位次的同学都去了哪里?全国各大学在辽宁2020-2024年各专业最低录取分数及录取位次数据,高考志愿必备参考数据
那些年,与你同分同位次的同学都去了哪里?全国各大学在辽宁2020-2024年各专业最低录取分数及录取位次数据,高考志愿必备参考数据