If you don't create any threads of your own, then no, you should not need to use the synchronized
keyword. That's assuming you're not writing J2EE servlets or filters, or a library that others (who are using multiple threads) can call.
Do any methods have to be synchronized if there's only the main thread?
-
26-06-2022 - |
Question
Do I really need to synchronize any methods if I'm not creating any threads (exception is the main). If so, can someone give a reason why and provide some examples please.
La solution
Autres conseils
No you dont need any synchronziation in this situation, synchronization is when multiple threads access shared data.
Synchronized methods are used when you don't want two threads to operate on any synchronized method at one time. If the application is single threaded there is no point taking a lock and adding an overhead of synchronization.
No, you should not as it will affect your performance. Synchronization should not be used if you are doing all in one thread.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow