两个线程同时运行案例
2016-11-28 13:56
225 查看
/** * 两个案例同时运行案例 * 1:这个两个线程并不是有规律的运行而是有没有规律的交替运行 */package com.test3;
public class Demo10_3 {
/** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Pig pig=new Pig(10); Bird bird=new Bird(10); Thread t1=new Thread(pig); Thread t2=new Thread(bird); t1.start(); t2.start(); }
}//打印class Pig implements Runnable{ int n=0; int times=0; public Pig(int n) { this.n=n; } public void run() { while(true) { try { Thread.sleep(1000); } catch (Exception e) { // TODO: handle exception } times++; System.out.println("猪说我是一个线程,在输出第"+times+"个hello, word"); if(times==n) { break; } } }}//算数学class Bird implements Runnable{ int n=0; int res=0; int times=0; public Bird(int n) { this.n=n; } public void run() { while(true) { try { Thread.sleep(1000); } catch (Exception e) { // TODO: handle exception } res+=(++times); System.out.println("鸟算的当前结果是"+res); if(times==n) { System.out.println("最后结果是"+res); break; } } }}
public class Demo10_3 {
/** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Pig pig=new Pig(10); Bird bird=new Bird(10); Thread t1=new Thread(pig); Thread t2=new Thread(bird); t1.start(); t2.start(); }
}//打印class Pig implements Runnable{ int n=0; int times=0; public Pig(int n) { this.n=n; } public void run() { while(true) { try { Thread.sleep(1000); } catch (Exception e) { // TODO: handle exception } times++; System.out.println("猪说我是一个线程,在输出第"+times+"个hello, word"); if(times==n) { break; } } }}//算数学class Bird implements Runnable{ int n=0; int res=0; int times=0; public Bird(int n) { this.n=n; } public void run() { while(true) { try { Thread.sleep(1000); } catch (Exception e) { // TODO: handle exception } res+=(++times); System.out.println("鸟算的当前结果是"+res); if(times==n) { System.out.println("最后结果是"+res); break; } } }}
相关文章推荐
- 两个线程同时运行案例
- 两个线程同时运行的案例
- 懒汉式的安全优化方式,两种方式。线程同时运行的时候,不会创建两个对象
- 在java中两个线程同时运行是怎么变化的
- 如何让两个线程同时运行
- 一台机器同时运行两个Tomcat
- 一台电脑上同时运行两个tomcat
- 一台机器同时运行两个或多个tomcat
- 如何同时运行两个配置,扩展完全不一样的 Firefox
- 一台机器同时运行两个或多个tomcat
- Problems with Multiple QApplications(同时运行两个qt embedded程序)
- 两个绿色版tomcat开机启动并同时运行
- 【案例分享】关于工程中同时链接了某库源码和该库so情况下,模块运行崩溃的问题跟进
- 一台机器同时运行两个Tomcat
- 同时运行两个mysql实例
- 同时运行两个TOMCAT的配置?
- [转]同时安装并运行.net 1.1 和 .net 2.0 两个版本的问题
- 同时运行两个动作 CCSpawn cocos2d
- linux下同时运行两个tomcat