Forums Register Login
Multi-Threading and when to use it
I am wondering at what areas of your code spawned their own threads explicitly? (I am sorry this is such a elementary question, but I just want to know how to get the best performance using multi-threading). Currently, the only place I programmatically spawn a new thread in only one place which is every half hour to do some writing and reading of the cached data (of course this is besides my main threads). I know RMI does some multi-threading under the hood and when networked there will be some concurrency. Did any of you guys spawn new threads in your code (besides the main methods). Once again I know this is a subjective and rather daft question to ask but I guess I am just an insecure bugger
Thanks Partners,
Hi Andy,

As a developer it's always nice to write some code that performs well. However, for this assignment I would't care that much, because the assignment says that a less performant design which is understandable for junior developers is preferred over a more complex, high performant one.

I read you perform some reading/writing of cached data every 30 minutes. That has advantages, because the possibility of data loss is reduced this way. Again, this is not a must requirement and it will complicate your code.

I launch an extra Thread when the application closes. A shutdown hook thread will write my cached data back to the .db file. This approach is more risky (than yours) due to the possibility of data loss, but it keeps my code more simple.

I guess our SCJD guru Roel will tell you this: KISS

Good luck with your implementation
I completely agree with Dennis. He's spot-on and I did exactly the same (just 1 extra thread as a shutdown hook).

And about your writing-cached-data-thread-every-30-minutes: nice solution to limit data loss to a minimum. I documented such a solution in my choices.txt as a way to solve the issue of possible data loss (one of the drawbacks of using a data cache). I didn't add such an implementation, because it was not a must requirement and makes your solution more complex, which violates the KISS principle (here it is ) of my application

Dennis Grimbergen wrote:I guess our SCJD guru Roel will tell you this: KISS

Right you are
Just wondering how there could be data loss? If I'm correct in understanding the general setup, the server reads the file into a cache at start up and writes back at shutdown. Is it if you have multiple servers? Do people test their solution with multiple servers, or just multiple clients?
What about a server crash? Or a power breakdown? All your data will be lost I suppose. If your server was up for a month before a power breakdown, you lose all your data for a month.
Thank you Dennis and Roel. That's a good idea, the shutdown hook. I kind of figured that this would be the case, I just wanted to make sure that there wasn't a standard practice for spawning threads in certain spots of your application. Well thank you much for the help guys, this site is awesome!!! I am starting part time school in 8 months so hopefully you guys are still here to answer my questions! I saw that I have a JavaScript class!! Prototypal languages confuse me so I hope you guys are still here and still brainiacs!!!
Thank you partners,

Andy Gertjejansen wrote:I saw that I have a JavaScript class!!

You'll discover that JavaScript has nothing to do with Java (if you didn't already know this). The appropriate forum for all your future JavaScript questions.
I was writing directly to the disk in my project, so I did not have to sync the cache from time to time. However I did use the shutdown hook as well, and other than that no manual thread spawning, RMI took care of all at the end of a day, socket solution would be different.
Thanks again. Yea I know JavaScript has absolutely nothing to do with java as I use it a lot with ajax requests and such. Thanks for showing me the appropriate forum Roel . I just thought maybe someone would crack a joke or something about prototypal languages. I think they are funny. You can do ANYTHING in them. lol but anywho thanks
Thank you partners,

This thread has been viewed 1019 times.

All times above are in ranch (not your local) time.
The current ranch time is
Dec 15, 2018 20:23:39.