首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

用署理模式处理海量高频数据更新

2012-08-28 
用代理模式处理海量高频数据更新业务背景: 海量高频数据(如股票实时报价), 更新的规则: 被更新的对象和更

用代理模式处理海量高频数据更新
业务背景: 海量高频数据(如股票实时报价), 更新的规则: 被更新的对象和更新方法都不一样.


下面是部分实例代码,最后一个是模拟的数据更新。


import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.view.View;import android.widget.TextView;public class TestActivity extends Activity implements RefreshHandlerInterface{private  Handler  _messageHandler = new Handler();@Overridepublic Handler getHandler() {return _messageHandler;}@Overridepublic 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();}}    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.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() || !TestActivity.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(TestActivity.this,CommonDefn.HIGHLIGHT_BACKGROUND_COLOR_INDEX, updateWrapper);        _messageHandler.postDelayed(refresh, 200);  //ready to for the current updating                            }          }      }.start();                                }}



这也也打包了,需要的可以看一下。

1 楼 sungod 2010-12-03   谢谢了,下来看看注释再多点就完美了。 2 楼 Coding.Ghost 2010-12-12   好吧.我承认.我看不太懂..

热点排行