这是类投资组合:
public class Portfolio extends Thread {
private volatile Thread stopMe= Thread.currentThread();
public Portfolio(String name) {
super(name);
// TODO Auto-generated constructor stub
}
public void stopMe(){
stopMe= null;
}
@Override
public void run() {
while(stopMe== Thread.currentThread()){
try {
for(int i=0; i<10; i++){
//sleep(700);
System.out.println("You have " +(500+i)+ " shares of IBM");
}
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println(Thread.currentThread().getName()+e.toString());
}}
}
}
这是我调用线程的地方
public class TestThreads {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MarketNews mn= new MarketNews("Market News");
mn.start();
//mn.setPriority(Thread.NORM_PRIORITY+1);
//mn.interrupt();
mn.isAlive();
Portfolio p= new Portfolio("Portfolio data");
p.start();
// System.out.println(Thread.currentThread().interrupted());
System.out.println("TestThreads is finished");
}
}
我调用了 p.start(),但它似乎不起作用。当我删除 stopMe 变量时,代码工作正常。