aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Multi-Threading and when to use it Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Multi-Threading and when to use it" Watch "Multi-Threading and when to use it" New topic
Author

Multi-Threading and when to use it

Andy Gertjejansen
Greenhorn

Joined: Dec 30, 2010
Posts: 16

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,
Andy


A+ 2009, OCPJP 6, (Working on: OCMJD 6)
Dennis Grimbergen
Ranch Hand

Joined: Nov 04, 2009
Posts: 140

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

SCJP, SCWCD, SCJD
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5286
    
  13

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


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

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?


SCJP (1.4 | 5.0), OCJP (6.0), OCMJD
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5286
    
  13

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.
Andy Gertjejansen
Greenhorn

Joined: Dec 30, 2010
Posts: 16

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
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5286
    
  13

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.
Elchin Asgarli
Ranch Hand

Joined: Mar 08, 2010
Posts: 222

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.


Personal page, SCJP 6 with 91%, SCWCD 5 with 84%, OCMJD
Andy Gertjejansen
Greenhorn

Joined: Dec 30, 2010
Posts: 16

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,
Andy
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Multi-Threading and when to use it