如何传递参数到Thread对象? [英] How to pass parameters to a Thread object?

查看:160
本文介绍了如何传递参数到Thread对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一个C ++类库提供了一个Thread基类,用户必须
实现一个 run()方法。 / p>

有没有推荐的方法如何传递参数到 run()方法?现在
我更喜欢通过构造函数(作为指针)传递它们。

解决方案

C ++,但是这就是你在Java中做的。你会有一个类扩展Thread(或实现Runnable)和一个构造函数,你想要传递的参数。然后,当你创建新的线程,你必须传入参数,然后启动线程,像这样:

  Thread t = new MyThread(args ...); 
t.start();

在您的情况下必须相同。


I'm working with a C++ class-library that provides a Thread base-class where the user has to implement a run() method.

Is there a recommended way on how to pass parameters to that run() method? Right now I prefer to pass them via the constructor (as pointers).

解决方案

I'm not sure about C++, but that's how you would do it in Java. You'd have a class that extends Thread (or implements Runnable) and a constructor with the parameters you'd like to pass. Then, when you create the new thread, you have to pass in the arguments, and then start the thread, something like this:

Thread t = new MyThread(args...);
t.start();

Must be the same in your case.

这篇关于如何传递参数到Thread对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆