线程结束之后的回调函数

需求是这样的
class A里面有个线程函数functionC();
class B里面调用A的functionC();并期望在线程结束后返回处理结果

实现过程
第一步:创建一个接口

public interface GetCallBack {
    public void onCountCallBack(int num);//回调的函数,里面的是你想要带回的参数
}

第二步:在你的线程结束后调用这个接口

public class B {
    public void Count(final int a, final GetCallBack getCallBack){
        new Thread(new Runnable() {
            @Override
            public void run() {
                //以下是个小例子,放你自己的处理逻辑
                int c_num=0;
                try {
                    for(int i=1;i<=a;i++){
                        c_num=c_num+i;
                    }
                }catch (Exception ex){

                }finally {
                    //线程结束调用回调函数,带回你想要的东西
                    if (getCallBack != null) {
                        getCallBack.onCountCallBack(c_num);
                    }
                }
            }
        }).start();
    }
}

第三步,调用线程函数的函数,带参带上回调函数

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        B b = new B();
        //带参带上回调函数
        b.Count(100, new GetCallBack() {
            @Override
            public void onCountCallBack(int num) {
                //回调的结果可会获取
                System.out.println("lalala回来的数字"+num);
            }
        });

    }
}

结果
在这里插入图片描述

一个小demo代码都在了,复制下来可以直接用的

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C++中,可以使用多线程来执行任务,同时也可以使用回调函数来处理任务执行后的结果。如果回调函数需要在不同的线程中被调用,需要考虑线程安全性和同步问题。 一种常见的做法是使用线程池来管理线程,每个线程都从任务队列中获取任务并执行。当任务完成后,线程将调用回调函数来处理任务执行的结果。为了确保回调函数线程安全性,可以使用锁来保护共享资源。 以下是一个简单的示例代码: ```c++ #include <iostream> #include <thread> #include <mutex> #include <condition_variable> #include <queue> using namespace std; class ThreadPool { public: ThreadPool(int numThreads) : stop(false) { for (int i = 0; i < numThreads; ++i) { threads.emplace_back([this] { while (true) { function<void()> task; { unique_lock<mutex> lock(mutex_); condition_.wait(lock, [this] { return stop || !tasks_.empty(); }); if (stop && tasks_.empty()) return; task = move(tasks_.front()); tasks_.pop(); } task(); } }); } } ~ThreadPool() { { unique_lock<mutex> lock(mutex_); stop = true; } condition_.notify_all(); for (thread& thread : threads) { thread.join(); } } template<class F> void enqueue(F&& f) { { unique_lock<mutex> lock(mutex_); tasks_.emplace(forward<F>(f)); } condition_.notify_one(); } private: vector<thread> threads; queue<function<void()>> tasks_; mutex mutex_; condition_variable condition_; bool stop; }; void callback(int result) { cout << "Result: " << result << endl; } int main() { ThreadPool pool(4); pool.enqueue([] { this_thread::sleep_for(chrono::seconds(1)); return 42; }, callback); return 0; } ``` 在上面的示例代码中,我们使用了一个线程池来管理线程。当需要执行任务时,我们可以将任务和回调函数一起放入任务队列中。每个线程都会从任务队列中获取任务并执行,当任务完成后,线程会将结果传递给回调函数。由于回调函数可能会被不同的线程调用,我们需要使用锁来保护共享资源。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值