线程小问题 本帖最后由 woshitianjin 于 2011-11-24 13:52:41 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你这个就是每隔10秒输出一次吗笨拙的代码new Thread(new Runnable(){ public void run(){ boolean flag=false; try { while(true){ while(flag){ System.out.println("123456789"); System.out.println(System.currentTimeMillis()); Thread.sleep(10000); } if(0==System.currentTimeMillis()%10000){//12点开始输出 flag=true; }else{ Thread.sleep(1);//1毫秒的判断一次 } } } catch (InterruptedException e) { e.printStackTrace(); } } }){}.start(); import java.text.SimpleDateFormat;import java.util.Date;public class SampleThread { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub runResult t=new runResult(); new Thread(t).start(); } } class runResult implements Runnable{ @Override public void run() { // TODO Auto-generated method stub while(true){ try { Thread.sleep(10000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } Date dt=new Date(); SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); sdf.format(dt); System.out.println(sdf.format(dt)+" 123456789"); } } }你试试吧 if(0==System.currentTimeMillis()%10000){//12点开始输出非要12点就改为if(0==System.currentTimeMillis()) linux 下 crontab 配置 调用你的类的main方法 import java.util.Date;import java.util.Timer;import java.util.TimerTask;public class Test2 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Timer timer = new Timer(); Date date = new Date(); date.setHours(0); date.setMinutes(0); date.setSeconds(0); Test2 t = new Test2(); timer.scheduleAtFixedRate(t.new MyTimerTask(), date, 24*60*60*1000); } class MyTimerTask extends TimerTask{ public void run() { //new 对象 } }} Eclipse出现错误,如何解决 Spring MVC 整合Freemarker的问题,求请教 推荐一本spring的书啊 [新手求助高人]关于j2ee学习的请教 spring2.5声明式事务及OpenSessionInView的问题——请教高手 数据权限控制思路 哪里有JSP语法 谁能帮我写个myeclipse +struts+hibernate的例子啊!急!急!急! javamail问题: 该段程序无法发送给Hotmail的用户 在一个应用中,如何衡量哪些对象使用ejb geoserver2.0发布postgis出错怎么处理啊 java 播放远程语音文件 + 播放多种格式语音文件
笨拙的代码
new Thread(new Runnable(){
public void run(){
boolean flag=false;
try {
while(true){
while(flag){
System.out.println("123456789");
System.out.println(System.currentTimeMillis());
Thread.sleep(10000);
}
if(0==System.currentTimeMillis()%10000){//12点开始输出
flag=true;
}else{
Thread.sleep(1);//1毫秒的判断一次
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}){}.start();
import java.text.SimpleDateFormat;
import java.util.Date;public class SampleThread { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
runResult t=new runResult();
new Thread(t).start();
}
}
class runResult implements Runnable{ @Override
public void run() {
// TODO Auto-generated method stub
while(true){
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Date dt=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.format(dt);
System.out.println(sdf.format(dt)+" 123456789");
}
}
}你试试吧
非要12点就改为if(0==System.currentTimeMillis())
import java.util.Timer;
import java.util.TimerTask;public class Test2 { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Timer timer = new Timer();
Date date = new Date();
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
Test2 t = new Test2();
timer.scheduleAtFixedRate(t.new MyTimerTask(), date, 24*60*60*1000);
} class MyTimerTask extends TimerTask{
public void run() {
//new 对象
}
}
}