public class PingPong extends Thread
{
private String word;
private int delay;
public PingPong(String whatToSay,int delayTime)
{
word = whatToSay;
delay = delayTime;
}
public void run()
{
try
{
while(true)
{
System.out.println(word);
Thread.sleep(delay);
}
}
catch(InterruptedException e)
{
return;
}
}
public static void main(String[] args)
{
new PingPong("ping",333).start(); //1/3 s
new PingPong("Pong",1000).start();//1/1 s
}
}
我的第一个JAVA程序
Java线程PingPong示例
最新推荐文章于 2025-11-30 18:24:05 发布
本文介绍了一个简单的Java线程示例程序,通过两个线程交替打印ping和pong来演示线程的基本用法。该程序利用了Java Thread类,通过sleep方法实现延时,展示了如何创建和启动线程。
2750

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



