2个回答
展开全部
用了3个类:写的比较简单,可以参考下
public class Running {
public static void main(String[] args) {
Tuzi tuzi = new Tuzi();
Wugui wugui = new Wugui();
tuzi.begin();
wugui.begin();
}
}
public class Wugui {
private Thread thread;
public void begin() {
thread = new Thread(new Runnable() {
public void run() {
while (true) {
move();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
thread.start();
}
private void move() {
System.out.println("乌龟跑了5米");
}
}
public class Tuzi {
private Thread thread;
public void begin() {
thread = new Thread(new Runnable() {
public void run() {
while (true) {
move();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("兔子醒了");
}
}
});
thread.start();
}
private void move() {
System.out.println("兔子跑了50米,开始睡觉");
}
}
public class Running {
public static void main(String[] args) {
Tuzi tuzi = new Tuzi();
Wugui wugui = new Wugui();
tuzi.begin();
wugui.begin();
}
}
public class Wugui {
private Thread thread;
public void begin() {
thread = new Thread(new Runnable() {
public void run() {
while (true) {
move();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
thread.start();
}
private void move() {
System.out.println("乌龟跑了5米");
}
}
public class Tuzi {
private Thread thread;
public void begin() {
thread = new Thread(new Runnable() {
public void run() {
while (true) {
move();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("兔子醒了");
}
}
});
thread.start();
}
private void move() {
System.out.println("兔子跑了50米,开始睡觉");
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
把需求再说清楚些
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询