每当一个用户注册成功,这个线程都会启动:
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()同步,谢谢,急等

解决方案 »

  1.   

    用线程池把, private static ExecutorService esc = Executors.newFixedThreadPool(100);  把每次注册放到线程池中;
      

  2.   

    用ExecutorService esc = Executors.newSingleThreadPool(100); 它能使注册在其中的线程同步
      

  3.   

    弄一个全局的锁。
    public static Lock lock;(比如是TOOLS类中,自己去初始化它)Tools.lock.lock();
    Volume volume = mcd.getVolume(userid, userName); 
    Tools.lock.unlock();