代理模式处理海量高频数据
2013-01-15 00:00
288 查看
代码一:
package com.night.proxymodehandlemasshighfrequencydataupdate;
/**
*@author night
*/
public interface CommonDefn {
public static int HIGHLIGHT_BACKGROUND_COLOR_INDEX=0xff0033ff;
public class DoThingsReturn{
public Object cmd;
public Object data;
public int errCode;
public DoThingsReturn(Object cmd,Object data,int errorCode){
super();
this.cmd=cmd;
this.data=data;
this.errCode=errorCode;
}
}
}
代码二:
package com.night.proxymodehandlemasshighfrequencydataupdate;
import android.os.Handler;
/**
* used for updating the background of the view while the data changed
*@author night
*/
public interface RefreshHandlerInterface {
//the proxy updating method
public void updateBackground(Object handlerId,int color);
//the proxy handler
public Handler getHandler();
}
代码三:
package com.night.proxymodehandlemasshighfrequencydataupdate;
import java.util.TimerTask;
import android.util.Log;
/**
* decrease alpha channel value from 255 to 0.
*@author night
*/
public class ColorRefreshTask extends TimerTask{
private RefreshHandlerInterface refreshHandler;
private final static int DELAY_ONCE=200;
private final static int TOTAL_RUNTIME=1500;
private final static int POWER_16_16=16*16*16*16*16*16;
private final static int INCREASE_ONCE=0xff / (TOTAL_RUNTIME/DELAY_ONCE);
private int color;
private Object id;
private int startTime;
private int alphaChannel;
/**
* @param color(current background color)
* @param id id of the updated view
*/
public ColorRefreshTask(RefreshHandlerInterface refreshHandler,int color,Object id){
super();
Log.d("color", "ready to set color");
this.color=color;
this.id=id;
this.startTime=0;
this.alphaChannel=0;
this.refreshHandler=refreshHandler;
}
public void run(){
int colorComm=color-0xff000000;//RGB color value;
int currColor=0;
if(startTime<TOTAL_RUNTIME){
startTime+=DELAY_ONCE;
alphaChannel+=INCREASE_ONCE;
currColor=POWER_16_16*alphaChannel+colorComm;
// Log.d("color", Integer.toHexString(currColor));
sendMsg(currColor);
refreshHandler.getHandler().postDelayed(this, DELAY_ONCE);
}else{
sendMsg(currColor<=color?color:0);
cancel();
}
}
public void startTimer(){
refreshHandler.getHandler().postDelayed(this, DELAY_ONCE);
}
private void sendMsg(final int currColor){
final Runnable myUpdateResults=new Runnable() {
@Override
public void run() {
refreshHandler.updateBackground(id, currColor);
}
};
new Thread(){
public void run(){
refreshHandler.getHandler().post(myUpdateResults);
}
}.start();
}
public void stopTimer(){
this.cancel();
}
}
代码四:
package com.night.proxymodehandlemasshighfrequencydataupdate;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity implements RefreshHandlerInterface{
private Handler _messageHandler=new Handler();
public Handler getHandler(){
return _messageHandler;
}
public void updateBackground(Object handlerId,int color){
if(!(handlerId instanceof CommonDefn.DoThingsReturn)){
return;//invalid parameter
}
else{
TextView current=(TextView)(((CommonDefn.DoThingsReturn)handlerId).data);
if(current.getVisibility()!=View.VISIBLE){
return;//no need to update for the view
}
if(color<=0){
current.setBackgroundDrawable(null);
current.setText(((CommonDefn.DoThingsReturn)handlerId).cmd.toString());
}else{
current.setBackgroundColor(color);
}
current.postInvalidate();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView tv=(TextView)findViewById(R.id.txtview);
new Thread(){
java.util.Random rand=new java.util.Random();
long lastUpdate=System.currentTimeMillis();
public void run(){
while(!Thread.interrupted()||!MainActivity.this.isFinishing()){
//模拟高频更新数据
if(System.currentTimeMillis()-lastUpdate<2000l){//设置两次动画的最少间隔时间
continue;
}else{
lastUpdate=System.currentTimeMillis();
}
CommonDefn.DoThingsReturn updateWrapper=new CommonDefn.DoThingsReturn(String.valueOf(rand.nextInt()), tv, 0);
ColorRefreshTask refresh=new ColorRefreshTask(MainActivity.this, CommonDefn.HIGHLIGHT_BACKGROUND_COLOR_INDEX, updateWrapper);
_messageHandler.postDelayed(refresh, 200);//ready to for the current updating
}
}
}.start();
}
}
package com.night.proxymodehandlemasshighfrequencydataupdate;
/**
*@author night
*/
public interface CommonDefn {
public static int HIGHLIGHT_BACKGROUND_COLOR_INDEX=0xff0033ff;
public class DoThingsReturn{
public Object cmd;
public Object data;
public int errCode;
public DoThingsReturn(Object cmd,Object data,int errorCode){
super();
this.cmd=cmd;
this.data=data;
this.errCode=errorCode;
}
}
}
代码二:
package com.night.proxymodehandlemasshighfrequencydataupdate;
import android.os.Handler;
/**
* used for updating the background of the view while the data changed
*@author night
*/
public interface RefreshHandlerInterface {
//the proxy updating method
public void updateBackground(Object handlerId,int color);
//the proxy handler
public Handler getHandler();
}
代码三:
package com.night.proxymodehandlemasshighfrequencydataupdate;
import java.util.TimerTask;
import android.util.Log;
/**
* decrease alpha channel value from 255 to 0.
*@author night
*/
public class ColorRefreshTask extends TimerTask{
private RefreshHandlerInterface refreshHandler;
private final static int DELAY_ONCE=200;
private final static int TOTAL_RUNTIME=1500;
private final static int POWER_16_16=16*16*16*16*16*16;
private final static int INCREASE_ONCE=0xff / (TOTAL_RUNTIME/DELAY_ONCE);
private int color;
private Object id;
private int startTime;
private int alphaChannel;
/**
* @param color(current background color)
* @param id id of the updated view
*/
public ColorRefreshTask(RefreshHandlerInterface refreshHandler,int color,Object id){
super();
Log.d("color", "ready to set color");
this.color=color;
this.id=id;
this.startTime=0;
this.alphaChannel=0;
this.refreshHandler=refreshHandler;
}
public void run(){
int colorComm=color-0xff000000;//RGB color value;
int currColor=0;
if(startTime<TOTAL_RUNTIME){
startTime+=DELAY_ONCE;
alphaChannel+=INCREASE_ONCE;
currColor=POWER_16_16*alphaChannel+colorComm;
// Log.d("color", Integer.toHexString(currColor));
sendMsg(currColor);
refreshHandler.getHandler().postDelayed(this, DELAY_ONCE);
}else{
sendMsg(currColor<=color?color:0);
cancel();
}
}
public void startTimer(){
refreshHandler.getHandler().postDelayed(this, DELAY_ONCE);
}
private void sendMsg(final int currColor){
final Runnable myUpdateResults=new Runnable() {
@Override
public void run() {
refreshHandler.updateBackground(id, currColor);
}
};
new Thread(){
public void run(){
refreshHandler.getHandler().post(myUpdateResults);
}
}.start();
}
public void stopTimer(){
this.cancel();
}
}
代码四:
package com.night.proxymodehandlemasshighfrequencydataupdate;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity implements RefreshHandlerInterface{
private Handler _messageHandler=new Handler();
public Handler getHandler(){
return _messageHandler;
}
public void updateBackground(Object handlerId,int color){
if(!(handlerId instanceof CommonDefn.DoThingsReturn)){
return;//invalid parameter
}
else{
TextView current=(TextView)(((CommonDefn.DoThingsReturn)handlerId).data);
if(current.getVisibility()!=View.VISIBLE){
return;//no need to update for the view
}
if(color<=0){
current.setBackgroundDrawable(null);
current.setText(((CommonDefn.DoThingsReturn)handlerId).cmd.toString());
}else{
current.setBackgroundColor(color);
}
current.postInvalidate();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView tv=(TextView)findViewById(R.id.txtview);
new Thread(){
java.util.Random rand=new java.util.Random();
long lastUpdate=System.currentTimeMillis();
public void run(){
while(!Thread.interrupted()||!MainActivity.this.isFinishing()){
//模拟高频更新数据
if(System.currentTimeMillis()-lastUpdate<2000l){//设置两次动画的最少间隔时间
continue;
}else{
lastUpdate=System.currentTimeMillis();
}
CommonDefn.DoThingsReturn updateWrapper=new CommonDefn.DoThingsReturn(String.valueOf(rand.nextInt()), tv, 0);
ColorRefreshTask refresh=new ColorRefreshTask(MainActivity.this, CommonDefn.HIGHLIGHT_BACKGROUND_COLOR_INDEX, updateWrapper);
_messageHandler.postDelayed(refresh, 200);//ready to for the current updating
}
}
}.start();
}
}
相关文章推荐
- 分治法+Hash处理海量日志数据
- Atitit 学习的本质 团队管理与培训的本质 attilax总结 1. 学习的定义 1 2. 学习的本质是数据的处理,海量的数据,处理能力有限的大脑 2 2.1. 摘要(缩小数据体量。。这个过程有
- 十道海量数据处理面试题与十个方法大总结
- 教你如何迅速秒杀掉:99%的海量数据处理面试题
- 海量大数据处理最新面试题-1
- Python从同一文件进行数据不落地的取高频处理
- 十道海量数据处理面试题与十个方法大总结
- 处理淘宝双11数据,使用pandas库快速切分海量csv文件
- 海量日志数据如何处理统计?
- 基于文件数据库的规则引擎处理海量高复杂度数据(一,业务背景)
- 基于文件数据库的规则引擎处理海量高复杂度数据(二,解决方案)
- 海量积分数据实时排名处理方式介绍一
- 海量文件或数据 导致高并发,高流量处理方案
- 海量数据处理
- java海量大文件数据处理方式
- 海量日志数据如何处理统计?
- 海量积分数据实时排名处理方式介绍二
- geotrellis使用(二十六)实现海量空间数据的搜索处理查看
- 处理 过滤高频数据的微小波动
- 海量数据处理专题(五)——堆