在java中,如何使用线程打印hello,每隔4秒打印一次,打印五次,然后停止.谢谢各位大侠
2个回答
展开全部
public class test extends Thread{
public void run()
{
int i = 1;
int max = 5;
while(true)
{
System.out.println("hello world");
i++;
if(i>max)
{
break;
}
try
{
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
test test1 = new test();
test1.start();
}
}
直接继承 Thread 类
public void run()
{
int i = 1;
int max = 5;
while(true)
{
System.out.println("hello world");
i++;
if(i>max)
{
break;
}
try
{
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
test test1 = new test();
test1.start();
}
}
直接继承 Thread 类
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询