1 要建立一个线程,第隔一秒打印一“hello world”?
解决方案 »
- 抓取图片像素后 图片显示出现了问题
- Call to 'super' must be the first statement in the constructor body..这句话是什么意思....
- 一个关于JFileChooser的问题
- 循环滚动条乍么做啊
- 我是初学者,请问像C中我们可以用scanf语句将一个数组从键盘输入,在JAVA中我该怎么实现呢?
- 一个installanywhere的问题,高手帮忙啊
- public Integer(String s)声明了会抛出NumberFormatException,为什么编译器不强制new Integer("s")要f写在try和catch里面
- 关于java中的url参数
- win98下如何配置JDK1.4?
- 很急!有关jacob的!
- 请教JTable
- 我现在有一个jar文件, 里面有类若干,我希望在里面加一个类, 然后重新生成jar
public void run() {
while (true) {
try {
Thread.sleep(1000);
} catch (Exception e) {
}
System.out.println("Hello world");
}
}}class PrintThread2 implements Runnable { public void run() {
while (true) {
try {
Thread.sleep(1000);
} catch (Exception e) {
}
System.out.println("Hello world");
}
}}public class TestX { public static void main(String[] args) {
// new PrintThread1().start();
new Thread(new PrintThread2()).start();
}
public static void main(String[] args) {
new Thread(){
public void run(){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
System.out.println("hello world");
}
}.start();
}
new Thread(){
public void run() {
while(true) {
try{
this.sleep(1000);
System.out.println("hello world");
}catch(Exception e) {
e.printStackTrace();
}
}
}
}.start();
}
不如试下这个
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;public class Test2 { public static void main(String[] args) {
ScheduledExecutorService schedule = Executors.newScheduledThreadPool(1);
schedule.scheduleAtFixedRate(
new PrintHelloWorld(), // 任务内容
1000 - System.currentTimeMillis() % 1000, // 延迟多长时间开始启动
1000, // 每隔多长时间执行一次
TimeUnit.MILLISECONDS // 时间单位
);
} private static class PrintHelloWorld implements Runnable { @Override
public void run() {
System.out.printf("%tT,%<tL %s%n", System.currentTimeMillis(), "hello world");
}
}
}