Do I need to worry about thread safe methods?
My boss at my new job is mentoring me on making my methods 'thread safe' and such, which I never had to worry about before. I don't understand what he's trying to say. I thought that 'thread safe' pertained to applications where you intentionally create threads. We are not creating any threads. He seems to think that a website has the potential of overwriting variables when multiple users log on. He likes to make methods static and then he started talking about the heap and stack where variables are stored. If we never create a thread, I don't see how that would happen...???
Recursion
(56,582 posts)And that's a surprising number of functions. Check the documentation; they'll usually say if they are re-entrant or not. If not, you have to write your own semaphore.
What platform are you on? Linux processes are actually "threads".
DaveJ
(5,023 posts)I still don't understand why there is any concern that a value will overwrite another value, unless the code is sharing a variable among threads. I would think that this would be well documented or handled by .Net if there were a constant danger of variables overwriting one another.
Recursion
(56,582 posts)That's the main advantage of Java and C# from a developer's perspective: the VM forces declaration of static members and methods, so that you can see exactly where you need to worry about concurrency.
If you're sure that there can be no two concurrent calls of any static methods (and anything that accesses a static variable is a static method), then you're fine. I'd actively prove it to myself, though.
In terms of why you care, the normal case for bugs is that a method stores some kind of recordkeeping data in a static variable. User A and user B try to write and read that at the same time, and the result is inconsistent. Inconsistency is a bug, and will eventually make something explode.
ManiacJoe
(10,136 posts)This is guaranteed to happen since your code is running inside the multi-threaded web server.
Sometimes sharing variables between threads is sometimes a good thing, sometimes a bad thing. Only you as the programmer knows which case your code falls into. If you do not want the variables shared, you need to code in a certain way to prevent it. It is actually quite easy do to; you just need to be aware of how you are coding things.
jeff47
(26,549 posts)Your application is running inside a web server. That web server will create multiple threads while serving requests.
As a result, your code could actually be running in multiple threads even though you are not creating any threads.
DaveJ
(5,023 posts)I just don't see anything to indicate that values in the code will overwrite values in other threads that .Net created servicing these requests.
napoleon_in_rags
(3,992 posts)You want to make sure your connection to the database is thread safe, then what's the worry? Of course you understand if your code is like
f = open_file("something.txt", "rb"
sleep(random_amount)
f.write(something)
f.close()
than you run the risk that a second user will click the same page while its still sleeping, and won't be able to access the file. But generally, if you keep all the data in functions or in the database, its safe.
DaveJ
(5,023 posts)I just don't get what the concern is over.
napoleon_in_rags
(3,992 posts)This is java we're talking about right? If I recall, static methods are called on the class, not the object. So then static attributes will appear for all pages simultaneously. Maybe that's created trouble for him in the past.
I thought the java model was to instantiate a servlet, (object) and then call its service() method in its own thread. Then all you have to worry about is external files, network connections, etc, just like an web app, you have to make sure no conflicts happen there. But that's fricking simple, its called the "synchronized" keyword:
http://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html