关于线程的小问题.路过的大侠帮我看下
gnodw 2004-05-11 05:37:11 import java.io.*;
public class ATM{
public static void main(String args[]){
ThreadA thread1=new ThreadA();
Thread thread2=new Thread(new ThreadB(),"SecondThread");
thread1.start();
try{
Thread.sleep(5000);
}
catch (InterruptedException e){
return;
}
if(thread1.isAlive())
{
thread1.stop();
thread2.start();
try{
Thread.sleep(5000);
}catch (InterruptedException e){
return;
}
if(thread2.isAlive())
thread2.stop();
else
System.out.println("程序结束按任意键继续!");
try{
System.in.read();
}
catch (IOException e)
{
System.out.println(e.toString());
}
}
}
class ThreadA extends Thread
{
int M=200;
ThreadA(){}
public void run()
{
System.out.println("您的帐户余额为:"+M);
try{
sleep(2000);
}catch (InterruptedException e)
{M=M+100;
return;
}
try{
sleep(2000);
}catch (InterruptedException e)
{
return;
}
System.out.println("您的帐户余额为:"+M);
}
}
class ThreadB implements Runnable{
int M;
ThreadB(){}
public void run(){
System.out.println("您的余额为"+M);
try {
Thread.sleep(2000);
}catch (InterruptedException e)
{M=M-50;
return;
}
System.out.println("您的帐户余额为:"+M);
}
}
}