[英]Java concurrency questions threads vs executorservice
I have been trolling the net the whole day. 我整天都在拖网。 Without a real answer. 没有一个真正的答案。 I'm hoping that someone can give me some advice. 我希望有人能给我一些建议。
What I am trying to do build is an auto buyer type application that will need to to poll auctions and check if items I want to purchase become available. 我要构建的是一个自动购买者类型的应用程序,它将需要轮询拍卖并检查我要购买的物品是否可用。
So conceptually I'm thinking that I should be able to start a thread that runs all the time (until I tell it to stop) 因此,从概念上讲,我认为我应该能够一直运行一个线程(直到我告诉它停止为止)
And when it finds an item it should start another thread that either bids or buys the item. 并且当它找到一个项目时,应该启动另一个线程来竞标或购买该项目。
I have the code all done that does the mechanics its just the threading I am stuck with. 我已经完成了所有的代码,这些代码确实完成了我坚持使用的线程的机制。
The first issue is i have been able to start threads using thread /runnable and also using execution services 第一个问题是我已经能够使用线程/ runnable和执行服务来启动线程
But in all instances I don't know how to get the thread to continue processing..... And then allow me to issue some sort of command that will stop the thread when I want to stop the program... 但是在所有情况下,我都不知道如何让线程继续处理.....然后让我发出某种命令,当我要停止程序时,该命令将停止线程...
Some guidance would be much appreciated 一些指导将不胜感激
As I understand you have next in mind: 据我了解,您接下来要考虑的是:
All of these could be implemented by your own thread framework using JVM thread primitives(and it would be a lot of fun to do so!). 所有这些都可以由您自己的线程框架使用JVM线程原语来实现(这样做会很有趣!)。 However I'd recommend to use Spring Framework for that. 但是我建议为此使用Spring Framework 。 Task Execution and Scheduling explains in detail how you can do that. 任务执行和计划详细说明了如何执行此操作。 In essence: 在本质上:
@Scheduled
on it! 只需将@Scheduled
放在上面! (Addresses #1) (地址1) As simple as it sounds you still need to understand what's going on under-hood and learn a bit about spring. 听起来很简单,您仍然需要了解引擎盖下的情况,并了解一些有关弹簧的知识。 Also @Sheculed
/ @Async
tasks are executed in two different thread pools and you might want to update default size values for them - lesson I've learned only after production deployment :-) 另外@Sheculed
/ @Async
任务是在两个不同的线程池中执行的,您可能想要更新它们的默认大小值-我仅在生产部署后才学到的教训:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.