TEAMMATES / teammates

This is the project website for the TEAMMATES feedback management tool for education
https://teammatesv4.appspot.com/
GNU General Public License v2.0
1.66k stars 3.28k forks source link

Can we use StringBuffer class instead of StringBuilder? #13055

Closed BigBrainTechnologies closed 5 months ago

BigBrainTechnologies commented 5 months ago

Discussed in https://github.com/TEAMMATES/teammates/discussions/12919

Originally posted by **elizabethoswald** March 21, 2024 Hi, In the src/client/java/teammates/client/scripts/ListActiveInstructors.java, can we use StringBuffer class instead of StringBuilder as the StringBuffer class is thread-safe one?
BigBrainTechnologies commented 5 months ago

Our students are ready to fix it, if the confirmation is obtained from key contributors on the validity of the defect

cedricongjh commented 5 months ago

This is not a defect at all as the code in question is single threaded

SwarnaLathaNatesan commented 5 months ago

If it is single threaded application, you mean to say, if there are multiple requests from multiple users, will it not get associated with a thread to complete the task?

SwarnaLathaNatesan commented 5 months ago

Here is the detailed set of points to be considered:

"Teammates" is using Servlet as the UI server, which is a Java-based technology for building web applications. Servlet is a part of the Java Enterprise Edition (Java EE) platform, which provides a comprehensive set of APIs for building enterprise-level applications. Regarding the question of whether it is possible to have an application designed without threading, the answer is no. Threading is an essential part of any modern application, including web applications. Threading allows an application to handle multiple tasks simultaneously, which is crucial for providing a responsive user interface and efficient data processing. In the case of the "Teammates" application, threading is used in the Servlet container, which handles incoming HTTP requests and dispatches them to the appropriate Servlet instances. Each Servlet instance runs in its own thread, allowing the application to handle multiple requests concurrently. Moreover, the "Teammates" application uses AngularJS for generating the UI, which is a JavaScript framework that runs in the client's web browser. AngularJS uses asynchronous JavaScript and XML (AJAX) requests to communicate with the server, which is another form of threading. AJAX requests allow the application to fetch data from the server without blocking the user interface, providing a smooth user experience. In summary, while it is possible to design an application without explicit threading, it is not possible to build a modern web application without using threading implicitly. The "Teammates" application uses Servlet and AngularJS, which both rely on threading to provide a responsive user interface and efficient data processing.

kevin9foong commented 5 months ago

If it is single threaded application, you mean to say, if there are multiple requests from multiple users, will it not get associated with a thread to complete the task?

Hi there, TEAMMATES does not use multi-threading in the application code in the sense that multiple threads do not perform operations on the same StringBuilder instance in parallel. Hence there is no race condition and no thread-safety is needed.

StringBuffer's thread safety also comes at a cost of slower operations compared to StringBuilder.