我有一個Client類調用MyService類的execute方法。該方法將依次調用一個InsertDAO類。這個InsertDAO具有實例變量的狀態。這是僅從MyService類的execute方法調用的。不能直接調用InsertDAO類。多個線程可以輸入到InsertDAO嗎?
我沒有創建任何線程,但我的App Server可能會在客戶端類上創建線程。現在,我想了解這將如何影響InsertDAO類。
- 多個線程可以同時訪問InsertDAO的對象嗎? - 是/否
- 當在客戶端類上生成線程時,MyService類的同一實例將被賦予給客戶端上的所有線程。然後每個線程都應該調用MyService的「execute」方法。這意味着每個線程都有自己的InsertDAO實例(我在MyService類的execute方法內部創建了InsertDAO對象)。如果是這樣,多個線程不能同時進入到InsertDAO。我的理解是否正確?
- 怎麼可能多個線程進入MyService.execute() - 一些解釋,如果這是真的。
- 多線程如何進入InsertDAO類? - 一些解釋,如果這是真的。
- 如何在不影響性能的情況下使線程安全?
專家,請分享您對此的看法。以下是我的代碼。
//code starts here
public class Client{
public void performExecution(){
InvoiceVO createInvoiceVO = new InvoiceVO();
MyService service = mew MyService();
createInvoiceVO = service.execute(createInvoiceVO);
//retrieve successful/failure information from createInvoiceVO
}
}
public class MyService{
public InvoiceVO execute(InvoiceVO createInvoiceVO){
InsertDAO insertDAO = new InsertDAO();
insertDAO.process(createInvoiceVO);
}
}
public class InsertDAO{
private List<LineItem> lineItemsList = new ArrayList<LineItem>();
private List<TaxVO> taxVOList = new ArrayList<TaxVO>();
private Connection connection = null;
public InvoiceVO process(InvoiceVO createInvoiceVO){
this.lineItemsList = createInvoiceVO.getLineItemsList();
this.taxVOList = createInvoiceVO.getTaxVOList();
connection = getConnection();
//insert tax vo objects
insertTaxVOObjects(taxVOList);
//insert line items
insertLineItems(this.lineItemsList);
//commit operation
//close connection
closeConnection();
}
private void insertTaxVOObjects(List<TaxVO> taxVOList){
//code to insert TaxVO objects
}
private void insertLineItems(List<LineItem> lineItemsList){
//code to insert LineItem objects
}
private void getConnection(){
//code to return connection
}
private void closeConnection(){
//code to close connection
}
}
編輯參考對象 – Gana