[code] public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Session session = HibernateSessionFactory.getSession();
Transaction t = session.beginTransaction();
List list = session.createQuery(
"from Student s where s.studentName='FrenchMan'").list();
synchronized (lock) {
if (list.size() == 0) {
if (i++ % 2 == 0) {//i是全局变量,操纵线程进入不同的条件分支
try {
lock.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
lock.notify();
}
Student s = new Student();
s.setStudentName("FrenchMan");
session.save(s);
}
}
t.commit();
session.close();
}
[/code]
throws ServletException, IOException {
Session session = HibernateSessionFactory.getSession();
Transaction t = session.beginTransaction();
List list = session.createQuery(
"from Student s where s.studentName='FrenchMan'").list();
synchronized (lock) {
if (list.size() == 0) {
if (i++ % 2 == 0) {//i是全局变量,操纵线程进入不同的条件分支
try {
lock.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
lock.notify();
}
Student s = new Student();
s.setStudentName("FrenchMan");
session.save(s);
}
}
t.commit();
session.close();
}
[/code]