UI线程上的多线程

时间:2015-07-29 02:34:36

标签: java android multithreading timer scheduled-tasks

我需要实时更新6张图表。所以我为每个图创建一个单独的队列,并尝试使用timer.schedule更新它,但它并没有真正起作用。特别是,如果我评论每个timer.schedule的调用,只留下第一个,它运行良好。但即使我添加第二个timer.schedule(仅限1和2个定时器),它也会冻结。这是我使用的代码:

timer_graph1 = new Timer();
timer_graph1.schedule(new Graph1(), 20, 20);
timer_graph2 = new Timer();
timer_graph2.schedule(new Graph2(), 20, 20);
timer_graph3 = new Timer();
timer_graph3.schedule(new Graph3(), 20, 20);
timer_graph4 = new Timer();
timer_graph4.schedule(new Graph4(), 20, 20);
timer_graph5 = new Timer();
timer_graph5.schedule(new Graph5(), 20, 20);
timer_graph6 = new Timer();
timer_graph6.schedule(new Graph6(), 20, 20);

class Graph1 extends TimerTask {

    public void run() {

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                //add data to the graph and update
                mChart1.invalidate();
            }
        });

    }
}

//same structure as for Graph1
class Graph2 extends TimerTask {

}

你能否说一下这种方法有什么问题?

1 个答案:

答案 0 :(得分:0)

从您的代码我可以告诉您,您的刷新并未更新为GUI或您的GUI快速闪烁。

更新GUI的经验法则是:

  1. 通过单独的线程在后端更新数据。如果需要,请使用双缓冲。
  2. 调用<?php //********************************************************************************** // Add the classes here by way of include() or just pasted at the top of the page... //********************************************************************************** // Start up the database connection MySQLConn::SetConnection(); // Assign database connection $handler = MySQLConn::$connect; // Checks if the submitted is a number. If so, isolates the ID and adds "where" clause $id = (!empty($_GET['ID']) && is_numeric($_GET['ID']))? " where ID = '".$_GET['ID']."'" : ""; // Add the $id to the end of the string // A single call would be SELECT * FROM guestquestionnaire where ID = '1' $query = $handler->query("SELECT * FROM guestquestionnaire{$id}"); $query->setFetchMode(PDO::FETCH_CLASS, 'guestquestionnaireEntry'); // Assign data while($r = $query->fetch()) { $setEntry[] = $r->entry; } // Implode with break if(isset($setEntry)) echo implode("<br />",$setEntry); ?> 内的所有GUI更新,以便在EDT线程内完成更新。