/* * ¹¦ÄÜ£ºÁ½¸öÏß³ÌÒ»ÆðÔËÐУ¬Æô¶¯½øÐвâÊÔ */ package com.bj.thread; public class MultiThread { 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 (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } times++; System.out.println("ÎÒÊÇÒ»¸öỊ̈߳¬ÔÚÊä³öµÚ"+times+"¸öPig"); if(times==n){ System.out.println("½á¹ûÊÇ"+times); break; } } } } class Bird implements Runnable{ int n=0; int res=0; int times=0; public Bird(int n){ this.n=n; } @Override public void run() { // TODO Auto-generated method stub while(true){ try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } res+=(++times); System.out.println("Birdµ±Ç°½á¹û£º"+res); if(times==n){ System.out.println("×îºó½á¹ûÊÇ"+res); break; } } } }