码迷,mamicode.com
首页 > 编程语言 > 详细

Java SE (6)之 多线程

时间:2014-07-16 19:27:01      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:blog   java   art   io   div   re   

package com.sunzhiyan03;
/*
 * 演示多线程
 * */
public class Demo3 {

	public Demo3() {
		// TODO Auto-generated constructor stub
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Pig pig = new  Pig();
		Bird bd = new Bird(10);
		//启动一个线程
		Thread pig_run = new Thread(pig);
		Thread bd_run = new Thread(bd);
		pig_run.start();
		bd_run.start();
	}

}

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 (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		res += (++times);
		System.out.println("结果是"+res);
		if(times == n){
			break;
		}
	  }
	}	
}
class Pig implements Runnable{
	int n = 0;
	int times = 0;
	public void run(){
		while(true)
		{
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("我是一个线程");
			times ++;
			if(times == 10){
				break;
			}
		}		
	}
	
}

  

Java SE (6)之 多线程,布布扣,bubuko.com

Java SE (6)之 多线程

标签:blog   java   art   io   div   re   

原文地址:http://www.cnblogs.com/sunxun/p/3841341.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!