我正面临着问题。我们正在将Java应用程序写为学校项目,我们应该使用线程。显然,应用程序并不像要求线程那么复杂,但我们必须拥有它们。
我们决定使用线程进行数据库通信,但我们不确定如何将它们用于单个方法。我们创建了数据库传输对象,其中包含一些将实体插入DB的方法请问如何使用Runnable
单一方法?更具体地说,我们想在方法中创建Runnable
然后将其添加到线程池中,但是,我们将能够返回返回。
public class EmployeeDTO {
private Employee employee;
private Connection conn;
private int id_employee;
/**
* Database connection
* @throws ClassNotFoundException
* @throws SQLException
*/
public EmployeeDTO() throws ClassNotFoundException, SQLException {
DatabaseConnection dbcon = new DatabaseConnection();
conn = dbcon.getConnection();
}
public EmployeeDTO(Employee employee) throws ClassNotFoundException, SQLException {
this.employee = employee;
DatabaseConnection dbcon = new DatabaseConnection();
conn = dbcon.getConnection();
}
/**
* Insert new Employee into database and sets it's id
* @param emp
* @throws SQLException
*/
public void insertEmployee(Employee emp) throws SQLException {
setEmployee(emp);
PreparedStatement pr = conn.prepareStatement("INSERT INTO employees "
+ "(nickname,name,surname,password_emp) "
+ "VALUES (?,?,?,?)", PreparedStatement.RETURN_GENERATED_KEYS);
pr.setString(1, employee.getNickname());
pr.setString(2, employee.getName());
pr.setString(3, employee.getSurname());
pr.setString(4, employee.getPassword());
pr.executeUpdate();
ResultSet rs = pr.getGeneratedKeys();
while (rs.next()) {
employee.setId_employee(rs.getInt(1));
}
}
/**
* Delete an employee from database
* @param id
* @throws SQLException
*/
public void deleteEmployee(int id) throws SQLException {
PreparedStatement pr = conn.prepareStatement("DELETE FROM employees WHERE id_employee=?");
pr.setInt(1, id);
pr.executeUpdate();
}
}
答案 0 :(得分:0)
更具体地说,我们想在方法中创建
Runnable
然后将其添加到线程池中,但是,我们将能够返回返回。
听起来您想将Callable
个任务(不是Runnable
)提交给线程池。当您向Callable<T>
提交ExecutorService
任务时,您将获得一个Future<T>
对象,您可以在以后使用该对象等待任务结果。
答案 1 :(得分:0)
您可能想要执行以下操作。您的insertEmployee方法将从call方法调用。
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.concurrent.Callable;
public class EmployeeDTO implements Callable<String> {
private Employee employee;
private Connection conn;
private int id_employee;
/**
* Database connection
* @throws ClassNotFoundException
* @throws SQLException
*/
public EmployeeDTO() throws ClassNotFoundException, SQLException {
DatabaseConnection dbcon = new DatabaseConnection();
conn = dbcon.getConnection();
}
public EmployeeDTO(Employee employee) throws ClassNotFoundException, SQLException {
this.employee = employee;
DatabaseConnection dbcon = new DatabaseConnection();
conn = dbcon.getConnection();
}
/**
* Insert new Employee into database and sets it's id
* @param emp
* @throws SQLException
*/
public void insertEmployee(Employee emp) throws SQLException {
setEmployee(emp);
PreparedStatement pr = conn.prepareStatement("INSERT INTO employees "
+ "(nickname,name,surname,password_emp) "
+ "VALUES (?,?,?,?)", PreparedStatement.RETURN_GENERATED_KEYS);
pr.setString(1, employee.getNickname());
pr.setString(2, employee.getName());
pr.setString(3, employee.getSurname());
pr.setString(4, employee.getPassword());
pr.executeUpdate();
ResultSet rs = pr.getGeneratedKeys();
while (rs.next()) {
employee.setId_employee(rs.getInt(1));
}
}
/**
* Delete an employee from database
* @param id
* @throws SQLException
*/
public void deleteEmployee(int id) throws SQLException {
PreparedStatement pr = conn.prepareStatement("DELETE FROM employees WHERE id_employee=?");
pr.setInt(1, id);
pr.executeUpdate();
}
/**
* Computes a result, or throws an exception if unable to do so.
*
* @return computed result
* @throws Exception if unable to compute a result
*/
public String call() throws Exception {
insertEmployee(employee);
return "SUCCESS";
}
}
在其他课程中,您可以使用下面的ExecutorService
try{
Set<Callable<String>> callables = new HashSet<Callable<String>>();
EmployeeDTO employee = new EmployeeDTO(emp);
callables.add(employee);
ExecutorService executorService = Executors.newFixedThreadPool(1);
List<Future<String>> futures = executorService.invokeAll(callables);
}catch (ExecutionException eEx) {
eEx.getMessage();
}finally{
executorService.shutdown();
}