怎样写个在指定时间段运行一个线程 时间段内线程运行,时间段外线程停止 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 没用过Timer,Timer好像只能定时执行,不能到时间停止吧?具体怎么搞,再弄个Timer去停? 在timer里面写new Thread{}的代码,做好控制就可以了。 Timer类是用来执行任务的类,接收一个TimerTask做参数Timer有两种执行任务的模式,最常用的是schedule,它可以以两种方式执行任务:1:在某个时间(Data),2:在某个固定的时间之后(int delay).这两种方式都可以指定任务执行的频率.例子:import java.io.IOException;import java.util.Timer;public class TimerTest{ public static void main(String[] args) { Timer timer = new Timer(); //在1秒后执行此任务,每次间隔2秒,如果传递一个Data参数,就可以在某个固定的时间执行这个任务. timer.schedule(new MyTask(), 1000, 2000); //测试停止此while循环 while(true) { try { int ch = System.in.read(); if(ch-'c'==0) { //退出任务 timer.cancel(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } static class MyTask extends java.util.TimerTask { public void run() { //TODO Auto-generated method stub System.out.println("________"); } }} 蛋疼的soap开发 java的awt图形碰撞问题 求救。如何提高prepareStatement的效率。大数据量 只会C Java Sql 的人可以找到一个软件工作么? 关于java 中文的问题? 关于一个日期格式的小问题,但是很急 关于SWING类中抛出的例外,大牛进 请问在JAVA里面怎么样调用win32 API函数?有没有现成的例子? thinking in java 第四版 中 类型信息一章P322 《thinking in java》的小疑问 使用 hibernate 时为什么要进行关系映射。 static的本质是什么,有关它本质的一切都可回答,谢谢了
import java.util.Timer;public class TimerTest
{
public static void main(String[] args)
{
Timer timer = new Timer();
//在1秒后执行此任务,每次间隔2秒,如果传递一个Data参数,就可以在某个固定的时间执行这个任务.
timer.schedule(new MyTask(), 1000, 2000);
//测试停止此while循环
while(true)
{
try
{
int ch = System.in.read();
if(ch-'c'==0)
{
//退出任务
timer.cancel();
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} static class MyTask extends java.util.TimerTask
{
public void run()
{
//TODO Auto-generated method stub
System.out.println("________");
}
}
}