package Demo08_01;
public class ThreadDemo1 implements Runnable{
static int money=10000;
public void run() {
Thread t=Thread.currentThread();
String name=t.getName();
while(money>0) {
for (int i = 0; i < 1000; i++) {
money-=5;
System.out.println(name+"吃饭花了5元,所剩余额为:"+money);
try {
if(name=="悟空") {
//休眠500毫秒,让出CPU
Thread.sleep(500);
}else {
Thread.sleep(300);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
//创建自定义线程对象
ThreadDemo1 th1=new ThreadDemo1();
//创建线程对象
Thread t1=new Thread(th1,"悟空");
//启动自定义线程对象
t1.start();
Thread t2=new Thread(th1,"八戒");
t2.start();
while(money>0) {
for (int i = 0; i < 1000; i++) {
money-=5;
System.out.println("唐僧"+"吃饭花了5元,所剩余额为:"+money);
try {
Thread.sleep(400);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}