每当一个用户注册成功,这个线程都会启动:
Thread t1 = new Thread(new MailUtil(userId,user));
t1.start();public MailUtil(String userId,User user){
this.userId = userId;
this.user = user;
}
public void run() {
synchronized (this){
System.out.println("线程启动");
MailAction ma = new MailAction();
ma.sendMailThread(userId, user);
}
}
public class MailAction {
public synchronized void sendMailThread(String userId,User user){
synchronized (this){
String title = "参与就有奖 梦圆五百万--活动中奖通知";
String content = "您在彩民村投注站举办的大派奖活动中获得如下奖品,请尽快领取并在有效期内使用,以下是您的礼品信息:";
MailDao md = new MailDao();
System.out.println("user.getEmail():"+user.getEmail()+"user.getUserName()"+user.getUserName());
//发送注册有奖邮件
md.sendMail(userId,user.getUserName(),user.getEmail(),title,content);
System.out.println("email send!!!!!!");
md.sendMail2(userId,user.getUserName(),user.getEmail(),title,content);
}
}
}public synchronized boolean sendMail(String userId, String userName, String email,
String title, String content) {
// 查询优惠卷数据库,得到优惠卷信息
Boolean flag =false;
Long userid = Long.parseLong(userId);
Volume volume = mcd.getVolume(userid, userName);public synchronized Volume getVolume(Long userId, String userName) {
Volume volume = new Volume();
String query = "select * from volume v,vol_type b where v.vol_id in (select min(vol_id) from volume where VOL_VALIDITY>=now() and VOL_STATUS=1) and v.vol_type=b.type_id";
String sql0 = "update volume set vol_status=?,send_user_id=?,send_account=?,used_times=1,use_times=0,vol_public_date=now() where vol_id =?";
Long status = Long.parseLong("0");
try{
synchronized(this){
this.getConnection();
PreparedStatement ps = con.prepareStatement(query);
ResultSet rs = ps.executeQuery();
if(rs==null)
return null; Long vol_id = Long.parseLong(volume.getId());
PreparedStatement ps0 = con.prepareStatement(sql0);
ps0.setLong(1,status);
ps0.setLong(2, userId);
ps0.setString(3,userName);
ps0.setLong(4, vol_id);
ps0.executeUpdate();
con.commit();
System.out.println("奖品数据库更新成功!");
}
} catch (SQLException e) {
e.printStackTrace();
现在的问题是查询时:getVolume()中同步不了,我的目的是让线程去取volume时同步起来,也就是让getVolume()同步,谢谢,急等
Thread t1 = new Thread(new MailUtil(userId,user));
t1.start();public MailUtil(String userId,User user){
this.userId = userId;
this.user = user;
}
public void run() {
synchronized (this){
System.out.println("线程启动");
MailAction ma = new MailAction();
ma.sendMailThread(userId, user);
}
}
public class MailAction {
public synchronized void sendMailThread(String userId,User user){
synchronized (this){
String title = "参与就有奖 梦圆五百万--活动中奖通知";
String content = "您在彩民村投注站举办的大派奖活动中获得如下奖品,请尽快领取并在有效期内使用,以下是您的礼品信息:";
MailDao md = new MailDao();
System.out.println("user.getEmail():"+user.getEmail()+"user.getUserName()"+user.getUserName());
//发送注册有奖邮件
md.sendMail(userId,user.getUserName(),user.getEmail(),title,content);
System.out.println("email send!!!!!!");
md.sendMail2(userId,user.getUserName(),user.getEmail(),title,content);
}
}
}public synchronized boolean sendMail(String userId, String userName, String email,
String title, String content) {
// 查询优惠卷数据库,得到优惠卷信息
Boolean flag =false;
Long userid = Long.parseLong(userId);
Volume volume = mcd.getVolume(userid, userName);public synchronized Volume getVolume(Long userId, String userName) {
Volume volume = new Volume();
String query = "select * from volume v,vol_type b where v.vol_id in (select min(vol_id) from volume where VOL_VALIDITY>=now() and VOL_STATUS=1) and v.vol_type=b.type_id";
String sql0 = "update volume set vol_status=?,send_user_id=?,send_account=?,used_times=1,use_times=0,vol_public_date=now() where vol_id =?";
Long status = Long.parseLong("0");
try{
synchronized(this){
this.getConnection();
PreparedStatement ps = con.prepareStatement(query);
ResultSet rs = ps.executeQuery();
if(rs==null)
return null; Long vol_id = Long.parseLong(volume.getId());
PreparedStatement ps0 = con.prepareStatement(sql0);
ps0.setLong(1,status);
ps0.setLong(2, userId);
ps0.setString(3,userName);
ps0.setLong(4, vol_id);
ps0.executeUpdate();
con.commit();
System.out.println("奖品数据库更新成功!");
}
} catch (SQLException e) {
e.printStackTrace();
现在的问题是查询时:getVolume()中同步不了,我的目的是让线程去取volume时同步起来,也就是让getVolume()同步,谢谢,急等
解决方案 »
- spring mvc + spring data jpa 带条件的查询分页的问题
- ireport子报表问题
- mybatis 连接mysql乱码
- 为什么不能获取其他TabPanel的值?
- Hibernate示例查询在多表操作遇到的问题?
- EJB到底是什么?
- Struts+hibernate+spring, 多模块Struts配置文件, 不能找到spring的DelegatingActionProxy?
- HTTP404,找不到servlet
- 怎样在windows服务中自动启动tomcat
- 在用小程序时间后没有通过SECURITY,有没有其他方法可以解决呀!
- myeclipse里面居然没有spring-webmvc.jar
- S2SH整合,请高手指点
public static Lock lock;(比如是TOOLS类中,自己去初始化它)Tools.lock.lock();
Volume volume = mcd.getVolume(userid, userName);
Tools.lock.unlock();