谁能告诉我一种方法,强制 Java 中的一个任务在允许开始下一个任务之前完成?具体来说,我想编辑下面的代码,以便在调用下一个标记的两行代码之前完全完成第一个标记的两行代码。
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String idString = req.getParameter("id");
Long id = new Long(idString);
//complete the actions specified on next two lines
School school = new SchoolDAO().findSchool(id);
req.setAttribute("school", school);
//before even starting the actions specified on the next two lines
List<CourseSummary> coursesummaries = new CourseSummaryDAO().findAllcsum(id);
req.setAttribute("coursesummaries", coursesummaries);
jsp.forward(req, resp);
}
<小时/>
编辑:
为了更好地理解 Fernando 的建议,我添加了 SchoolDAO 的一些相关部分,如下所示:
public class SchoolDAO extends DataAccessObject{
public School findSchool(Long id) {
ResultSet rs = null;
PreparedStatement statement = null;
Connection connection = null;
try {
connection = getConnection();
String sql = "select * from schoolprog where id=?";
statement = connection.prepareStatement(sql);
statement.setLong(1, id.longValue());
rs = statement.executeQuery();
if (!rs.next()) {return null;}
return readSchool(rs);
}
catch (SQLException e) {throw new RuntimeException(e);}
finally {close(rs, statement, connection);}
}
private School readSchool(ResultSet rs) throws SQLException {
Long id = new Long(rs.getLong("id"));
String spname = rs.getString("spname");
String spurl = rs.getString("spurl");
School school = new School();
school.setId(id);
school.setName(spname);
school.setUrl(spurl);
return school;
}
}
同样,CourseSummaryDAO 包含:
public class CourseSummaryDAO extends DataAccessObject{
public List<CourseSummary> findAllcsum(Long sid) {
LinkedList<CourseSummary> coursesummaries = new LinkedList<CourseSummary>();
ResultSet rs = null;
PreparedStatement statement = null;
Connection connection = null;
try {
connection = getConnection(); //this is the line throwing null pointer error
String sql = "select * from coursetotals where spid=?";
statement = connection.prepareStatement(sql);
statement.setLong(1, sid);
rs = statement.executeQuery();
//for every row, call read method to extract column
//values and place them in a coursesummary instance
while (rs.next()) {
CourseSummary coursesummary = readcsum("findAll", rs);
coursesummaries.add(coursesummary);
}
return coursesummaries;
}
catch (SQLException e) {throw new RuntimeException(e);}
finally {close(rs, statement, connection);}
}
程序中断的行是:
connection = getConnection(); //
最佳答案
如果您有两项任务需要串行执行(即一项任务在下一项任务开始之前完成),那么最好的答案是同步执行它们。例如,假设 task1()
和 task2()
是任务:
// Wrong way:
Runnable r1 = new Runnable(){
public void run() {
task1();
}};
Runnable r2 = new Runnable(){
public void run() {
// Wait for r1 to finish task1 ... somehow
task2();
}};
// Right way:
Runnable r = new Runnable(){
public void run() {
task1();
task2();
}};
就您的情况而言,看起来 doGet 调用只有在获得两个任务的结果时才能返回。因此,这表明在这种情况下您根本不应该使用线程。只需在请求线程上依次调用 task1()
和 task2()
即可。
编辑
查看 doGet
方法以及您随后添加的两个类,看起来处理过程已经是顺序/串行的。也就是说,第一个“任务”在第二个“任务”开始之前结束。
getConnection()
引发 NullPointerException
的问题(很可能)与异步无关。但是,如果没有看到 getConnection()
的代码和完整堆栈跟踪,我就无法确定这一点。
关于java - 确保一个任务在 java 中另一个任务开始之前完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18157950/