【问题标题】:Android TextWatcher read SQL data with multi threading. how close old thread?Android TextWatcher 使用多线程读取 SQL 数据。如何关闭旧线程?
【发布时间】:2020-07-17 15:18:51
【问题描述】:

我有一个通过创建新线程连接到数据库的功能,因为搜索功能很繁重。

textView.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(final Editable editable) {
        search(textView.getText().toString());
    }
});

public void search(String word) {
    new Thread(new Runnable() {
        @Override
        public void run() {
            this.wordsList = db.getWordsList(word); //big job
            .
            .
            .
        }
    }).start();
}

但是当这些代码在短时间内执行多次时,程序会变慢。多次执行上述代码,程序挂起。

所以我想关闭前一个线程,然后打开新线程。实际上我不需要以前线程的结果,所以我想关闭它们以提高程序速度。

【问题讨论】:

    标签: java android multithreading android-studio textwatcher


    【解决方案1】:

    我使用这段代码解决了我的问题:

    Executor executor = Executors.newSingleThreadExecutor();
    executor.execute(new Runnable() { public void run() { /* do something */ } });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-01
      • 1970-01-01
      • 2013-08-20
      相关资源
      最近更新 更多