简体   繁体   English

Java并发性问题线程与executorservice

[英]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: 据我了解,您接下来要考虑的是:

  1. Have thread which polls from time to time some service in order to find bargains 有不时轮询某些服务的线程以查找便宜货
  2. If found then another thread should be spawned for this concrete item for doing bidding, buying and pissing off other other buyers who don't have such a great tool. 如果找到,则应为该具体项目生成另一个线程,以进行出价,购买和惹恼其他没有这么好的工具的其他买方。
  3. Stopping particular thread if you don't want to buy item anymore. 如果您不想再购买商品,则停止特定线程。

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: 在本质上:

  • Want to have some method executed periodically? 是否想定期执行某些方法? Just put @Scheduled on it! 只需将@Scheduled放在上面! (Addresses #1) (地址1)
  • Want to have some method executed in asynchronous manner? 是否想以异步方式执行某些方法? Put @Async!(Addresses #1) 放@Async!(地址#1)
  • Regarding #3 - cancelling/stopping threads is problem by its own. 关于#3-取消/停止线程本身就是问题。 Take a look into this question/answer. 看看这个问题/答案。 The best(and recommended) way I found so far is to use a shared variable in memory or some value in database (if application runs in distributed environment) as a flag which asks the background thread to stop. 到目前为止,我发现的最佳(推荐)方法是使用内存中的共享变量或数据库中的某个值(如果应用程序在分布式环境中运行)作为要求后台线程停止的标志。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM