public class PingPong {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
Thread t = new Thread() {
@Override
public void run() {
pong();
}
};
t.start(); // not t.run();
// Thread.sleep(3000);
// t.join(); //wait it finish
System.out.print("Ping");
}
private static void pong() {
System.out.print("Pong");
}
}
PingPong or PongPing
最新推荐文章于 2022-11-11 20:11:45 发布
这是一个简单的Java程序,演示了如何使用多线程实现PingPong效果。主线程打印Ping,而子线程负责打印Pong。通过启动子线程并在主线程中等待子线程结束来实现交互式的打印效果。
2723

被折叠的 条评论
为什么被折叠?



