2013-11-02 27 views
0

停在那里是类:如何要求,而从另一个类

public class M16 
{ 
    public boolean on = true; 
    void shoot() 
    { 
    System.out.println("Bang"); 
    } 
    void pullTrigger() 
    { 
    while(on) shoot(); 
    } 
} 

我想设置我的标志后我打电话pullTrigger()方法。像这样:

M16 m = new M16(); 
    m.pullTrigger(); 
    Thread.sleep(2000); 
    m.on = false; 

这是一个简单的问题,但我不知道我需要做什么。

回答

1

实际上,您不能从相同的Thread中断whileThread.sleep(2000)将永远不会执行,因为m.pullTrigger()将永远运行。您的场景更可能发生在多线程设置中。有numerous SO topics covering this area,例如this one

+0

thx,现在我可以制定我的问题了。 – user2929711