整理:java定时器。
本文纯属个人思路,如有错误,请指正。
java的Timer依赖Thread,每一个Timer实际上都是一个Thread。
import java.util.TimerTask; /**
* 本类仅为实现TimerTask,意义不大。
* @author 9082046**@qq.com
*
*/
public class Task extends TimerTask
{
public void run()
{
System.out.println(this.hashCode());
}
}
在win7 的myeclipse8.5的默认安装后的未做任何调节的开发环境下:
方案一:
import java.util.Timer; /**
* 启动1w个Timer
* @author 9082046**@qq.com
*
*/
public class TestTimer
{
public static void main(String[] args)
{
add(10000);
}
public static void add(int amount)
{
for(int index=0;index < amount; index ++)
{
Timer timer=new Timer();
timer.schedule(new Task(), Integer.MAX_VALUE);
}
}
}
启动1w个的Timer,结果如下:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAswAAACBCAIAAABb6nvWAAAfvUlEQVR4nO2dzZWruhKFiUsBKY4OgZmnL4GeKoAOwhOHcIOoNxCIUqmqEBhs47O/1euue7CsPzDalIT28D8AAAAAgBMYCAAAAADgBCAyAAAAAHAKEBkAAAAAOAWIDAAAAACcAkQGAAAAAE5hoIH9hfHd9SEiojjQ+Lhe/mOgYaB0fMYaqet8pUjDQDFNdXuy1f39tq/cx0hDfKaCm8nVe75ndhAP+tE5+UT2006JhoGGQKWh+Ry98SdfOv/T7j8nEev+57zxOgQv4+xxTfAx4xFr9mN86Y88vHY4ORy1/mN41UklorR+vh7jdOfKI8rL6vaucvcxhlff3ONAce6UMXT97tTrzcmn/Tnnk1LSx/D+3yDv+Rfff95CMERG5pDr8O3ntJOr1HMr72rXB49HWWQkeXuann7mxx0uwPmTU/nNlK9wORMGGgKNcf5IS1wKyjiKPpT0sUocozzuoOa/NR+//uOo5BPmQqcKRCUrfgpSXI7zqvL0qVtkTOcx0GM+EuZqTAXN9bHK3dpvbbk+YdDLVesT2ZGSIHeFVX+f9uau5uNdJ2m5yPMF75TejqZxoFQ/eURWtHW9WflQHcMonfMYKabpZsT/n7Tr0L9OSPs9knGdT5nPl0H+KJcy9XxzJVu/F+v4pv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CyeKaXe17QwvV8wWBkiwO2tbepZzLalfx8eNe1naaJjOotUzH9obVXPSi7gh3X89fOS5mwiGnKqiznTFbRdv+07fXz7zwvq9cP7736MV2fg6jXSA7LlIExZ0Hae5hm/nzOZf6oTEDEe86qLOecdVdbtMynd7AnOd6zet5ivImspndDiPrSK+WWNaTaWtqlFfWkFRZ+AuBxcZEBzqB/BgGchAjz/KvIIMoHgldYAXCByACMaD3Wg5fz72wKImGRj55FJ28EYQwA1oDIAAAAAMApQGR8GMr7lp0rS8B30uz5jb+P+Hv3dQHANYDIAAAAAMApQGQAAAAA4BQgMgAAAABwChAZAAAAADgFiAwAAAAAnAJEBgAAAABOASIDAAAAAKcAkQEAAACAU4DIAAAAAMAp/B/JVjeM81NYEgAAAABJRU5ErkJggg==" alt="" />
才1w个Timer提示jvm的内存不够使的了。
方案二:
/**
*
*/
package linked_array; import java.util.Random;
import java.util.Timer; /**
* @author 908204694@qq.com
*
*/
public class TestTimer
{
public static void main(String[] args)
{
add(10000);
}
public static void add(int amount)
{
Timer timer=new Timer();
for(int index=0;index < amount; index ++)
{
timer.schedule(new Task(), Integer.MAX_VALUE);
}
}
}
同一个Timer调度1w个TimerTask,至少在运行5分钟后没出什么Error。。。。貌似有点囧,也没任何输出,写的Timer调度TimerTask的延迟时间有点大,哈。
在实际应用中,Timer存在计时器线程终止 或者 计时器取消 导致的 IllegalStateException
,单个Timer 或许不太适合长时间 调度 非重复事件 TimerTask。
原因:对 Timer 对象最后的引用完成后,并且 所有未处理的任务都已执行完成后,计时器的任务执行线程会正常终止(并且成为垃圾回收的对象)。但是这可能要很长时间后才发生。出自:jdk api 1.6.0 java.util 类 Timer。
方案三:
/**
* @author 9082046**@qq.com
*
*/
public class User
{
private int user_id;
// 用来标识时间
private int time_stamp;
public User(int userId, int timeStamp)
{
user_id = userId;
time_stamp = timeStamp;
} public int getUser_id()
{
return user_id;
} public int getTime_stamp()
{
return time_stamp;
} public void setTime_stamp(int timeStamp)
{
time_stamp = timeStamp;
}
} /**
* @author 9082046**@qq.com
*
*/
public class Test_Timer
{ private static ArrayList<User> list=new ArrayList<User>();
private static Random randam=new Random(); // 计时用,每秒加一。或者直接用时间戳吧。
private static int timeS = 0;
private static final int tenS = 10; public static void main(String[] args)
{
add(10000); while(true)
{
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{ }
traveral();
timeS++;
}
} public static void add(int amount)
{
for(int index=0;index < amount; index ++)
{
int random = randam.nextInt(1000);
User user =new User(index,random);
list.add(user );
}
} /**
* 遍历全部的玩家。
*/
public static void traveral()
{
int amount=list.size();
User user = null;
for(int index=0;index < amount; index ++)
{
user = list.get(index);
if(user.getTime_stamp() < timeS)
{
System.out.println("userId:"+user.getUser_id() +"," +user.getTime_stamp());
user.setTime_stamp(user.getTime_stamp()+tenS);
}
}
}
}
使用Thread.sleep() + 遍历全部数据实体并比较时间标记位 :模拟计时器。
个人注:
①、 主动让系统回收垃圾对象:System.gc()。
②、方案二 和 方案三 都存在缺陷, 如果存在长耗时的任务,会导致后续的部分任务 晚于预设的时间标识点才能执行。
整理:java定时器。的更多相关文章
- java定时器和多线程实践记录
这几天因为需要测试mongodb读写分离的问题,因此写了个定时查询程序,并且用到了多线程,以达到定时启动多个线程查询数据库的效果,下边代码记录备忘: package timmer; import ja ...
- java定时器,Spring定时器和Quartz定时器
一.java定时器的应用 其实java很早就有解决定时器任务的方法了,java提供了了类java.util.TimerTask类基于线程的方式来实现定时任务的操作,然后再提供java.util.Tim ...
- Java定时器:Timer
项目中往往会遇到需要定时的任务,例如订单,当用户在某个规定时间内没有操作订单时,订单状态将会发生改变. 那么在这种情况下,我们会用到定时器. 举例: import java.util.Timer; / ...
- Java定时器应用
在Java多线程中,有的时候,我们需要按照指定间隔时间来执行一些任务,这时,我们就要用到定时器.我们在这里以Java中的Timer定时器为例,演示定时器的应用. 请看下述代码: import java ...
- java定时器实现总结
前言:Java定时器目前主要有3种实现方式:JDK组件,Spring Task,Quartz框架. 1. JDK组件(1) java.util.TimerTask MyTimerTask.java: ...
- Java定时器TimeTask
package com.alan.timer; import java.util.Calendar;import java.util.Date;import java.util.Timer;impor ...
- Java定时器Timer
Java定时器Timer在JDK库中,Timer类主要负责计划任务的功能,也就是在指定的时开始执行某一个任务.Timer类的主要作用就是设置计划任务,但封装任务的类却是TimerTask类,执行计划任 ...
- JAVA定时器实现之一(通过继承TimerTask)
在某些时候, 我们需要实现这样的功能,某一程序隔一段时间执行一次,而这一事情由系统本身来完成,并不是人为的触发,我们一般可称此为定时器任务. 这类技术主要应用到那些需要进行后台整理数据的系统中,比如说 ...
- [Java定时器]用Spring Task实现一个简单的定时器.
今天做一个项目的的时候需要用到定时器功能.具体需求是: 每个月一号触发一次某个类中的方法去拉取别人的接口获取上一个月份车险过期的用户.如若转载请附上原文链接:http://www.cnblogs.co ...
随机推荐
- Java 中断线程
- oracle 查询本周数据生成下周数据
CreateTime--2018年1月4日16:38:01 Author:Marydon oracle 查询本周数据生成下周数据 第一步:查询指定区间数据 第二步:改变查询结果的值 查询日期字段+ ...
- 关于Javascript表单验证
//验证字符串非空 var Validator = { VerityLib: { IsNotEmpty: function (input) { if ...
- 【TP3.2】路由匹配和规则
TP3.2框架的路由匹配和规则处理: 包括:静态路由,动态路由,多参数路由.正则路由 <?php return array( //'配置项'=>'配置值' /* * 路由开启和匹配.首先开 ...
- PHP代码优化之缓存(转)
我们在编写程序时,总是想要使自己的程序占用资源最小,运行速度更快,代码量更少.往往我们在追求这些的同时却失去了很多东西.下面我想讲讲我对PHP优化的理解.优化的目的是花最少的代价换来最快的运行速度与最 ...
- c#中用DirectShow实现媒体播放器
原文地址:https://www.cnblogs.com/aiqingqing/p/4338448.html 用.net做多媒体开发的似乎不多,所以网上资源也少,看的人更少.不过我的博客上居然还有几位 ...
- 跨域JSONP原理及调用详细演示样例
上篇博客介绍了同源策略和跨域訪问概念,当中提到跨域经常使用的基本方式:JSONP和CORS. 那这篇博客就介绍JSONP方式. JSONP原理 在同源策略下,在某个server下的页面 ...
- Atitit.国际化中的日期与钱符号问题
Atitit.国际化中的日期与钱符号问题 1. 用户名注册的问题 1 1.1. 不能限制用户名长度与特殊符号 1 2. 2.8月7号未必总写成8/7,有时也用7/8 2 3. 5.$未必总是指美元 3 ...
- 亲热接触Redis-第一天
引言 nosql,大规模分布式缓存遍天下.Internet的时代在中国由其走得前沿,这一切归功于我国特色的电商. 因此nosql.大数据技术在中国应用的比国外还要前沿. 从这一章開始我们将開始进入到真 ...
- commit
git blame -L 260, 270 a.xml no permissions fastbootsudo chown root:root fastbootsudo chmod +s fast ...