I have been designing a small java application to pick up scripts from stock market and watching them and wanted to intimate if price crosses our level. When I created single java file to watch, it is some what delay if there aremore stocks picked (taking time for parsing, checking conditions and all).
Now i thought of creating separate thread for each stocks. so i hope there wont be any delay and we get at right time.. Could you some one guide if there is any better way than this?
But here also i wanted to collected information for all stocks and wanted to display in single frame and it has to refresh every 10 seconds... Any idea or any better design than this or something like that would be great help..thanks in advance
Adding more Threads to handle different stock quotes could help improve speed, but there will be a limit based on other bottleneck situations (processor, network connection, etc...). So you would have to test and see. My thought would be to use a variable sized ThreadPool and a Queue with all the stock quotes in it. Each Thread in the Pool takes a stock, processes it, updates the current value, and puts it back at the end of the Queue. You can then quickly compare the number of Threads you need to most efficiently grab the data.
It may be that it just isn't fast enough. It depends on what you are using to get the data. You may think about using a service which provides a Market Data API for gathering the data. You may find it more efficient than whatever method you have now which requires parsing.
As for the display - keep that separate. You could have a SwingTimer which triggers a refresh of the screen. Collect all the stock quotes into a single list and the refresh simply iterates and displays the value. You might also think about using an Observable and have individual prices update when they get modified, rather than a period refresh. Either way, I would have a 'Data Transfer Object' - one for each quote being watched. The JFrame holds a list of all of them but only calls a 'get' method to retrieve values. Meanwhile each parsing task/thread knows about just one of the DTOs and uses a 'set' method to update the value.
Joined: Nov 30, 2010
Thanks Steve for your information. Since all threads are running in every seconds how can we put into single object and display them in JFrame. is should be synchronized?
Yes, there probably needs to be some synchronization involved. For the simple case, you could probably just synchronize get... and set... methods for the stock quote. There are other options (such as using volatile or Atomic references).