对 JAVA 多线程的理解
2015-06-28 17:03
387 查看
所谓多线程,就是计算机同时做几件事,对外表现的是同时完成。
就实现了多线程问题
实现方法
在 JAVA 中就非常比较简单的了,只需要将新建的类实现 runable 方法,在类中的 run 方法中写自己要完成的事情。一个例子
package com.mingsoft; import java.text.SimpleDateFormat; import java.util.Date; public class TestThread3 { public static void main(String args[]) { Counter c1 = new Counter(1); Thread t1 = new Thread(c1); Thread t2 = new Thread(c1); Thread t3 = new Thread(c1); Counter c2 = new Counter(2); Thread t4 = new Thread(c2); Thread t5 = new Thread(c2); Thread t6 = new Thread(c2); TimeDisplay timer = new TimeDisplay(); Thread t7 = new Thread(timer); t1.start(); //t2.start(); //t3.start(); t4.start(); //t5.start(); //t6.start();*/ t7.start(); } } class Counter implements Runnable{ int id; public Counter(int id ) { // TODO Auto-generated constructor stub this.id = id; } public void run() { int i = 0; while (i++ <= 10) { System.out.println("ID: " + id + " No. " + i); try { Thread.sleep(10); } catch (InterruptedException e) { // TODO: handle exception } } } } class TimeDisplay implements Runnable{ public void run() { int i = 0; while (i++ <=3) { System.out.println( new SimpleDateFormat().format(new Date())); try { Thread.sleep(40); } catch (InterruptedException e) { // TODO: handle exception } } } }
就实现了多线程问题
相关文章推荐
- struts2设置默认首页
- java 粘贴图片
- struts2.1笔记02:servlet简介
- spring 与 CXF 整合 webservice 出现error “Unable to locate Spring NamespaceHandler for XML schema namespace” 总结
- Java项目依赖的配置过程
- Java项目依赖的配置过程
- java学习之旅37--面向对象_10_this隐式参数_内存分析
- Spring spEL
- JAVA对象的强,弱,虚引用
- spring 初始化报错
- struts2.1笔记01:MVC框架思想浅层理解
- The import org.jdom cannot be resolved解决办法
- Java的native关键字
- 使用java自带md5加密(转载)
- 3.struts架构剖析
- 每天学点java性能优化之字符串处理优化
- Spring 使用外部部署文件
- Spring的bean注入方式
- Java中的泛型(2)
- java 服务提供者框架