The question is the following. Suppose I have a class A that works with multiple threads. In the class A constructor, I create an instance of another class B.

Instances of class B for each thread will be individual?

I will give for example the following code:

public class EmployeeService implements Service<Employee> { private Dao<Employee> employeeDao; private Dao<Phone> phoneDao; private static ThreadLocal<Connection> connectionTL = new ThreadLocal<>(); private void initTransaction() { try { connectionTL.set(pool.getConnection());//коннекшн берется из пула соединений } catch (PoolException e) { throw new ServiceException(e); } } @Override public void editEmployee(Employee obj) { initTransaction(); Connection connection = connectionTL.get(); //по логике в этом месте data race не должно быть ибо для каждого потока будет свой экземпляр дао. //и коннекшины не должны перетерться employeeDao.setConnection(connection); //проставляю connections для DAO слоя phoneDao.setConnection(connection); ...Логика вызова методов класса B } public class AbstractDao<T> implements DAO<T>{ private Connection connection; @Override public void setConnection(Connection connection) { this.connection = connection; } @Override public T edit(T obj) throws DaoException { try (PreparedStatement preparedStatement = connection.prepareStatement(getSqlStringEdit())) { ... логика запросов к БД } catch (SQLException e) { throw new DaoException(e); } return newObj; } } 
  • 2
    Not. Relationships between objects are defined by references to each other in memory. Having a link, any thread can access the methods of the object. What you write about is singleton-per-thread & nbsp; & mdash; Singleton pattern that creates one instance per stream. It needs to be organized on its own. - Mark Shevchenko
  • That is, I correctly understood that if I in class A methods first put down some values ​​to non-static fields of class B, and then, in the same method, I invoke a class B method that works with this field, then Data Race should not be? - KnockKnock
  • one
    This code can be a race condition. When you editEmployee connection will be unique for each thread. But employeeDao and phoneDao are the same in all instances of the EmployeeService class. If editEmployee is launched from different streams, then one connection will be recorded in employeeDao , then another (second stream). In this case, the first thread will write there without suspecting anything. - Mark Shevchenko
  • But from my question and your first answer it follows that if I create in the EmployeeService constructor instances of the EmployeeDao class, public EmployeeService() { employeeDao = new EmployeeDao(); } public EmployeeService() { employeeDao = new EmployeeDao(); } then for each thread, an instance of EmployeeDAO will be created and, in theory, they should not even know about each other. That is, one thread establishes a connection ( connection ) to its copy, the other to its own. - KnockKnock
  • one
    I see in your code that ThreadLocal declared only to Connection. EmployeeDao not thread local, and only one instance on EmployeeService . - Mark Shevchenko

0