Java中线程只能执行某些方法吗?
我正在努力为机器人制作一个界面。我的机器人类具有包括移动、停止移动和读取传感器数据的方法。如果可能的话,我希望某些方法在给定线程下运行,某些其他方法在另一个线程下运行。我希望能够发送命令以移动到 robot
对象,让执行它的线程休眠 duration
毫秒,然后停止移动,但我希望可以调用 stop()
方法并中断正在执行运动的线程。非常感谢任何帮助。
public class robotTest
{
public static void main(String[] args) throws InterruptedException
{
Robot robot = new Robot(); //Instantiate new Robot object
robot.forward(255, 100, Robot.DIRECTION_RIGHT, 10); //Last argument representing duration
Thread.sleep(5000); //Wait 5 seconds
robot.stop(); //Stop movement prematurely
}
}
I'm working on making an interface for a robot. My Robot class has methods that include movement, stopping movement and reading sensor data. If at all possible, I'd like to have certain methods run under a given thread and certain other methods run under another. I'd like to be able to send the command to move to the robot
object, have the thread executing it sleep duration
milliseconds and then stop movement, but I'd like the stop()
method able to be called and interrupt the thread executing the movement. Any help is greatly appreciated.
public class robotTest
{
public static void main(String[] args) throws InterruptedException
{
Robot robot = new Robot(); //Instantiate new Robot object
robot.forward(255, 100, Robot.DIRECTION_RIGHT, 10); //Last argument representing duration
Thread.sleep(5000); //Wait 5 seconds
robot.stop(); //Stop movement prematurely
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我建议使用 ExecutorService 实例化您的 Robot 类,您可以使用它来异步移动。将移动请求提交到您的服务并使用返回的 Future 来“停止”移动请求。
I would suggest instantiating your Robot class with an ExecutorService that you can use for moving asynchronusly. Submit the movement request to your service and use the Future returned to 'stop' the move request.
如果我理解正确,那么是的,您可以从任何给定线程调用对象上的方法。然而,为了以无错误的方式工作,机器人类需要 线程安全。
If I'm understanding you correctly then yes, you can call methods on an object from any given thread. However, for this to work in a bug free fashion the robot class needs to be thread safe.
确保对 Robot 的所有调用都来自具有调用权限的线程(扩展您创建的 Thread 的类)。将此方法添加到您的调用中。
注意:这段代码远非完美。但它可能会给您一些可以在您的应用程序中使用的想法。
Make sure all your calls to Robot come from a thread (a class extending Thread that you create) with permissions to make the calls. Add this method to your call.
Note: this code is far from perfect. But it may give you some ideas you can use in your application.
当您实例化一个处理移动的
Robot
时,您必须启动一个新的后台线程。线程将坐在那里,等待来自转发或停止的信号并执行适当的操作。您必须使用信号量、等待句柄或其他线程间通信元素来同步线程。
最不健壮的解决方案,浪费了最多的CPU(这是伪代码,因为我有一段时间没有使用Java,可能与.NET API混合在一起):(
上面的代码应该修改为Java以获得更好的答案)
出了什么问题使用以下代码:
You have to start a new background thread when you instantiate a
Robot
that would handle movement. The thread would sit there, waiting for a signal from forward or stop and do the appropriate thing.You will have to synchronize the threads using either semaphores, wait handles, or other inter thread communication elements.
The least robust solution that wastes the most CPU (this is pseudo code since I have not used Java in a while, might be intermixed with .NET APIs):
(the above code should be modified to be Java for better answer)
What is wrong with this code: