Przejdź do głównej zawartości

StringBuilder vs Stringbuffer

StringBuilder naleyży używać jesli zamierzamy concat wiele stringów, StringBuilder został wprowadzony od Javy5.  StringBuffer to starsza wersja StringBuildera. StringBuffer trobi to samo jednak z punktu widzenia performance jest wolniejszy gdzyż jest thread safe


Thread Safety in Java 
Thread safety in java is the process to make our program safe to use in multithreaded environment, there are different ways through which we can make our program thread safe.

  •  Synchronization is the easiest and most widely used tool for thread safety in java. 
  • Use of Atomic Wrapper classes from java.util.concurrent.atomic package. For example AtomicInteger 
  • Use of locks from java.util.concurrent.locks package. 
  • Using thread safe collection classes, check this post for usage of ConcurrentHashMap for thread safety. 
  • Using volatile keyword with variables to make every thread read the data from memory, not read from thread cache.

Język Java również posiada słowo kluczowe volatile, lecz ma ona nieco inną specyfikację:

  • We wszystkich wersjach Javy istnieje globalna kolejność odczytów i zapisów do zmiennej z volatile. Dzięki temu każdy wątek mający do niej dostęp odczyta jej obecną wartość przed kontynuacją, zamiast (potencjalnego) wykorzystania zmiennej przechowywanej w pamięci podręcznej. 
  • Od Javy 5 zmienne z modyfikatorem volatile zachowują relację happens-before. Używanie volatile może być szybsze niż blokowanie, ale może też nie działać w niektórych wypadkach.

Komentarze

Popularne posty z tego bloga

Stored Procedures - JDBC Java SQL

Stored Procedures: group of SQL statements that perform a particular task  Normally created by DBA Can have any combination of input, output, and input/output parameters Benefits: Performance  - compiled once but executable more times Productivity and Ease of Use  - avoid redundant code, extend SQL Database functionality  Scalability   Maintainability Interoperability Replication Security To call stored procedure from Java The JDBC API provides the CallableStatement CallableStatement myCall = myConn.prepareCall("{ call some_stored_procedure() }"); ... myCall.execute(); JDBC API parameter types IN default INOUT OUT Stored Procedure can return result sets EXAMPLE: Created stored procedure on MySQL side - NAME:  increase_salaries_for_department DELIMITER $$ DROP PROCEDURE IF EXISTS ` increase_salaries_for_department `$$ CREATE DEFINER=`student`@`localhost` PROCEDURE `increase_salaries_for_departme...

Skrócony zapis if - instrukcja warunkowa java

Instrukcja warunkowa - warunek i rezultat. if (warunek) { jesli spełniony wykonań operacje i zwróć wynik; } warunek nie spełniony Możliwości skrócenia kodu instrukcji warunkowej if (i < 0) ? i-- : i++; Jeżeli i mniejsze od zera to i-- jezeli false to i++ if (i < 0) {     i--; } else {     i++; } Skrócony zapis instrukcji warunkowej else if (i < 0) ? i--;  inna_zmienna=4; : i++; if (i < 0) {     i--; } else {     i++;     inna_zmienna = 4; } Skrócony zapis if

Inserting Data into SQL Database with Java

Inserting Data into SQL Database with Java Java Documentation - Processing SQL statements with JDBC  Process: Get connection to database Create a statement Execute SQL query import java.sql.*; /**  *  * @author www.luv2code.com  *  */ public class JdbcTest { public static void main(String[] args) throws SQLException { Connection myConn = null; Statement myStmt = null; ResultSet myRs = null; String dbUrl = "jdbc:mysql://localhost:3306/demo ?autoReconnect=true&useSSL=false "; String user = "root"; String pass = "Sasanka01"; try { // 1. Get a connection to database myConn = DriverManager.getConnection(dbUrl, user, pass); System.out.println("Database connection successfully created"); // 2. Create a statement myStmt = myConn.createStatement(); int rowsAffected = myStmt.executeUpdate(                          "...