求解一个多线程问题 1 要建立一个线程,第隔一秒打印一“hello world”? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 class PrintThread1 extends Thread { @Override 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(); } public static void main(String[]args) throws Exception { 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.Executors;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"); } }} 求教:Java打包成jar遇到的问题 jvm报异常!不知道应不影响服务? 各位帮帮忙啊!!! 两个窗口对话的问题 感谢maoerzuozuo,来领分吧 关于applet的小问题 做聊天室,用Socket好?还是DatagramSocket呢? 关于响应鼠标,键盘事件的处理不明白,请大虾解释一下。谢谢! 关于String类的问题,帮个忙谢谢! 请问哪里有OA(办公自动化)功能之一"工作流"的在线演示? 请教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");
}
}
}