第二种:自定义类MyThread 实现Runnable接口,主函数创建5个线程,自定义线程类的构造函数输出 创建线程X * run方法内统计每个线程 一共6次
2016-04-12 22:37
801 查看
package com.heima.gu; import java.util.HashMap; public class Test03 { /**自定义类MyThread 实现Runnable接口,主函数创建5个线程,自定义线程类的构造函数输出 创建线程X * run方法内统计每个线程 一共6次 * 用hashmap 集合 统计每个线程的次数,创建五个集合。 * @param args */ public static void main(String[] args) { int i; for(i = 1; i <= 5;i++){ new Thread(new MyThread("线程"+i),"线程"+i){ }.start(); } } } class MyThread implements Runnable{ public MyThread(String str){ System.out.println("创建"+str); } int count = 6; @Override public void run() { HashMap<String, Integer> hm = new HashMap<String, Integer>(); hm.put(Thread.currentThread().getName(), 0); while(true){ synchronized (MyThread.class) { if(hm.get(Thread.currentThread().getName()) >= count){ break; }else{ hm.put(Thread.currentThread().getName(), hm.get(Thread.currentThread().getName())+1); System.out.println(Thread.currentThread().getName()+" :计数"+ hm.get(Thread.currentThread().getName())); } for (String str : hm.keySet()) { System.out.println("**"+str+"*******"+hm.get(str)); } } } } }
相关文章推荐
- 关于C++中 虚函数 多态 和 访问限定符的思考
- Java集合(二):List列表
- Spring源码分析——BeanFactory和ApplicationContext
- c++第三次作业
- 用户管理
- warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead.
- 20135202闫佳歆--week 7 深入理解计算机系统第七章--读书笔记
- 2016年4月12日
- iOS JsonModel 的使用
- [线程]——任务调度
- 升级完python2.7.11后问题
- F5负载均衡及会话保持学习笔记二(紧接上篇)
- c++实验3-个人所得税的判断
- 自定义类MyThread 实现Runnable接口,主函数创建5个线程,自定义线程类的构造函数输出 创建线程X * run方法内统计每个线程 一共6次
- JVM调优总结(二)-一些概念
- [c++11]如果函数不修改传入的参数,则参数添加const 修饰 -- rvalue
- Applying Sampling Theory To Real-Time Graphics
- 显示意图和隐式意图选择
- CSUST 第九届校赛I-奇怪的电梯(DFS)
- mysql导入导出可以支持表情