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;
}
}
} }

  

上一篇:Clear all username or password for login.


下一篇:linux系统监控与硬盘分区/格式化/文件系统管理