0

当来自数据库的新对象到达时,将创建一个新的处理程序,该处理程序将对象发送到 UI 线程,以便 UI 处理和绘制对象。

    public void notify(MyObject myObject) {
        Handler handler = new Handler(Looper.getMainLooper());
        Runnable runnable = new Runnable() {
            @Override
                public void run() {
                    myview.handleUpdate(myObject); 
                }
        };
        handler.post(runnable);
    }

这很有效,但是当同时有 100 条消息进来时,将创建 100 个线程,这会造成瓶颈。

我想让处理程序在启动前等待 2 秒,这样它就可以等待其他对象。

我尝试了以下方法,但这不起作用。线程将被启动,但等待 2 秒等待将添加到列表中的其他对象。当时间结束时,线程将与所有对象一起发布到主线程。

    boolean wait = false;
    ArrayList<MyObject> myObjectList = new ArrayList<>();

    public void notify(MyObject myObject) {
        if(wait) {
            myObjectList.add(myObject); // handler waits already, just add the object to the list
            return;
        }

        wait = true; // says that the handler waits for data
        myObjectList.add(myObject);
        Handler handler = new Handler(Looper.getMainLooper());
        Runnable runnable = new Runnable() {
            @Override
                public void run() {
                    Thread.sleep(2000); // wait for 2 seconds
                    myview.handleUpdate(new ArrayList<>(myObjectList)); 
                    wait = false; // posted, a new handler can be created
                    myObjectList.clear(); 
                }
        };
        handler.post(runnable);
    }
4

2 回答 2

0
 Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
      @Override
      public void run() {
        //Do something after 2 seceonds
      }
    }, 2000);
于 2018-12-25T10:34:06.747 回答
0
public void notify(MyObject myObject) {// this is the ONLY method on WORKER THREAD
            waitList.add(myObject);
 }

ArrayList<MyObject> waitList = Collections.synchronizedList(new ArrayList<>());

Handler handler = new Handler();// main thread handler

Runnable runCheck = new Runnable() {
           @Override
           public void run() {// runs on main thread
             if(checking){
                 for(Iterator<MyObject> iter = waitList.listIterator(); iter.hasNext(); ){
                      myview.handleUpdate((MyObject)iter.next());
                      iter.remove();
                       // instead of removing you can add a boolean (dirty) 
                       //to MyObject and set it to false to flag it as considered object
                      //in this way you do not need to change list from multiple threads
                 } 
                 checkWaitList();// check for new items 2 seconds later
             }
 }
 private void checkWaitList(){// runs on main thread
      if(checking)
           handler.postDelayed(runCheck, 2000);
 }

 private boolean checking;

 private void startChecking(){// runs on main thread
    if(!checking){
        checking = true;
        checkWaitList();
    }
 }
 private void stopChecking(){// runs on main thread
    checking = false;
    handler.removeCallbacks(runCheck);
 }
于 2018-12-25T20:33:01.003 回答