简体   繁体   English

是否可以在同一活动中执行单独的网络线程而无需等待?

[英]Is it possible to perform separate network thread within a same activity without wait?

I am performing two class which is extending ASyncTask and both have different functions but because of the second class my first class is lagging. 我正在执行两个类,这些类正在扩展ASyncTask并且都具有不同的功能,但是由于second类,我的第一类比较滞后。 So what i want to know is, is there any better solution to code in such a way that both of the operation will perform the task without making other operation to wait? 因此,我想知道的是,有没有一种更好的代码解决方案,使得两种操作都可以执行任务而无需等待其他操作?

Updated with code For the first call in the onCreate() 已更新代码,用于onCreate()的首次调用

new connection().execute(); //

Some task performed by the same class called 由同一个类执行的某些任务称为

 public class connection extends AsyncTask {
    @Override
    protected Object doInBackground(Object... arg0) {
       //some operation
        return value;
    }

    @Override
    protected void onPostExecute(Object o) {
        super.onPostExecute(o);
        String m = String.valueOf(o);
        if (o != null) {
           someoperation
        } else {
            edittxt.setTextColor(Color.RED);
            edittxt.setText("No Internet Connection");
        }
    }
}

similarly i am performing the other class that i have. 同样,我正在表演另一门课。

You can create two separate threads and perform your operations. 您可以创建两个单独的线程并执行您的操作。 It will quarantine, that all operations will be performed async. 它将隔离所有操作将异步执行。

final Handler handler = new Handler();

        Thread operation1 = new Thread(new Runnable() {
            @Override
            public void run() {
                doOperation1();
                handler.run(new Runnable(){
                    @Override
                    public void run() {
                        onPostExecute1();
                    }
                });
            }
        });

        Thread operation2 = new Thread(new Runnable() {
            @Override
            public void run() {
                doOperation2();
                handler.run(new Runnable(){
                    @Override
                    public void run() {
                        onPostExecute2();
                    }
                });
            }
        });

        operation1.start();
        operation2.start();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以在同一线程中等待回调? - Is it possible to wait for a callback in the same thread? 如何在同一线程中对第二个 Kafka 消费者成功执行轮询? - How to successfully perform poll on second Kafka consumer within the same thread? 是否可以等待活动结果? - Is it possible to wait for activity results? 是否可以使用 Project Reactor 在不阻塞线程的情况下等待事件? - Is it possible to wait for a event without blocking a thread with Project Reactor? 在单独的片段中实现两个RecyclerView,但在同一活动中 - Implementing two RecyclerViews in separate fragments, but within the same activity 等待并在同一线程上通知 - Wait and Notify on same Thread 从单独的线程开始活动? - Starting activity from a separate thread? 静态网络服务器:可能的最短等待时间:低活动:节点JS与裸露的基于Java线程 - Static webserver: Shortest possible wait: Low activity: Node JS vs bare bones Java thread based 如果我使用单独的线程进行日志记录,它的性能会更好吗? - If I use a separate thread for logging, will it perform better? Future是否需要在单独的线程中执行计算? - Does Future require to perform the computation in a separate thread?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM