multithreading - Must these short methods be synchronized in Java? -


consider following static helper

public class dbutil {      private static final logger logger = logmanager.getlogger(dbutil.class);      public static void closeall(resultset rs, statement stmt, connection conn) {         close(rs);         close(stmt);         close(conn);     }      public static void close(connection connection) {         if (connection != null) {             try {                 connection.close();             } catch (sqlexception e) {                 logger.error(null, e);             }         }     }      public static void close(statement statement) {         if (statement != null) {             try {                 statement.close();             } catch (sqlexception e) {                 logger.error(null, e);             }         }     }      public static void close(resultset resultset) {         if (resultset != null) {             try {                 resultset.close();             } catch (sqlexception e) {                 logger.error(null, e);             }         }     }      public static void closeany(object o) {         if(o == null) return;          try {             if (o instanceof statement) {                 statement stmt = (statement) o;                 stmt.close();             } else if(o instanceof connection) {                 connection conn = (connection) o;                 conn.close();             } else if(o instanceof resultset) {                 resultset rs = (resultset) o;                 rs.close();             } else {                 logger.warn("unknown object type");             }         } catch(sqlexception e) {             logger.error(null, e);         }     }  } 

this class can used multiple thread. must methods synchronized or of them , why?

actually i'm not sure possible after resultset != null check 1 thread formal parameter(s) changed different object instance(s) thread , resultset, statement or connection closed (in case methods not synchronized).

i'm not sure possible after resultset != null check 1 thread formal parameter(s) changed different object instance(s) thread

no, method arguments have same semantics local variables. private single method invocation , cannot mutated other thread.

if each of calling threads uses objects own thread-local connection, there no issues. methods not share state in case.


Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -