Java SE (6)之 多线程

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)之 多线程,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。