使用thread类
2015-10-23 15:44
302 查看
package thread; public class Example_1 { public static void main(String args[]){ Home home=new Home(); Thread dog,cat; home.setFood(20); cat=new Thread(home); dog=new Thread(home); dog.setName("狗"); cat.setName("猫"); cat.start(); dog.start(); } } class Home implements Runnable{ int foodAmount; public void setFood(int w){ foodAmount=w; } public void run(){ while(true){ String name=Thread.currentThread().getName(); if(name.equals("狗")){ System.out.println(name+"吃饭"); foodAmount-=2; } else if(name.equals("猫")){ System.out.println(name+"吃饭"); foodAmount-=1; } System.out.println(" 剩 "+foodAmount); try{ Thread.sleep(2000); } catch(InterruptedException e){} if(foodAmount<=0) return ; } } }
相关文章推荐
- iOS开发系列--地图与定位
- 今天就注册上海ORACLE2用户组014在峰会酒吧!
- jdk安装,配置
- 一个简单至极的PHP缓存类代码
- 大三了,剩下的时间如何规划
- animate.css 动画制作
- EditText 限制可输入的字符
- 获取版本号和清除缓存
- 判断两个apk的签名一致
- ActivityManager: Warning: Activity not started, its current task has been brought to the front 的的问题
- Android使用Intent一键分享图片文字到腾讯、新浪、开心、微信等
- QSqlDatabase 连接 cloneDatabase()
- 微信浏览器缓存问题
- 字符串处理
- poj3669 流星雨
- java中的Scanner类进行操作
- Codeforces Round #274 (Div. 1) C. Riding in a Lift 前缀和优化dp
- HTML5 自定义属性 data-* 和 jQuery.data 详解
- JavaScript之 ------ JavaScript与html的结合方式
- MFC 串口发送文件