这里是程序,那位dx帮我看看。public class Game {
public static void main(String args[]){
PingPong table = new PingPong();
Thread alice = new Thread(new Player("bob", table));
Thread bob = new Thread(new Player("alice", table));
Thread t = new Thread();
alice.setName("alice");
bob.setName("bob");
alice.start();
bob.start();try{
//Wait 5seconds
t = Thread.currentThread();
t.yield();
Thread.currentThread().sleep(5000);}
catch(InterruptedException e) {}table.hit("DONE");
try {
Thread.currentThread().sleep(100);}
catch(InterruptedException e) {
}
}}
而你这个程序有两个线程,一定要用xxx.currentThread()进行判断,当前是那个线程运行。
public void run()
{
if(Thead.currentThread()==alice)
{
alice.sleep(5000);
}
else if(Thead.currentThread()==bob)
{
..
..}
public static void main(String args[]){
PingPong table = new PingPong();
Thread alice = new Thread(new Player("bob", table));
Thread bob = new Thread(new Player("alice", table));
Thread t = new Thread();
alice.setName("alice");
bob.setName("bob");
alice.start();
bob.start();
}试下看好用么
哪个sleep,跟Thread.sleep()效果都一样的