我的线程Dump中阻塞状态中的线程太多。这使得我的应用程序无响应。当我查看代码时,我发现函数是同步的,因为哪个线程进入阻塞状态。如果没有这个方法同步然后多个线程同时访问相同的方法会导致问题,如一个邮件线程干扰另一个线程。如何以我的应用程序不必遭受的方式更改我的代码。这是我的代码 -
// Mailtransport类
public final boolean openConnection() throws MailingException {
boolean flag = false;
if (transport == null) {
mailsession = MailSession.getMailSession();
try {
transport = (SMTPTransport) mailsession.getNewTransport();
flag = true;
// System.out.println("\n--MailTransport.java ------------ Connection is SUccessful -- "+transport.isConnected());
} catch (MailingException mex) {
setStatusmessage(new StringBuffer(statusMessages[1]).append(
" -- ").append(mailsession.getSmtphost()).append(
"<BR> Contact").append(
"<A HREF=\"mailto:" + returnPath + "\">").append(
"Administrator").append("</A>").toString());
throw mex;
}
} else {
flag = true;
}
return flag;
}
// MailSession类
public synchronized Transport getNewTransport() throws MailingException
{
try
{
return getNewTransport(mailtype);
} catch (MailingException e)
{
throw e;
} catch (Exception e)
{
throw new MailingException(" Exception in MailSession.getTransport ", e);
}
}
public static MailSession getMailSession()
{
MailSession _mailSession = MultiTenancyUtil.getTenantMailSession();
if (_mailSession == null)
{
synchronized (MailSession.class)
{
if (_mailSession == null)
{
_mailSession = new MailSession();
_mailSession.initialize();
MultiTenancyUtil.getTenantResources().setMailSession(_mailSession);
}
}
}
return _mailSession;
}
public Transport getNewTransport(String type) throws MailingException
{
Transport transport = null;
try
{
transport = session.getTransport(type);
if (StringUtil.isvalidInteger(getSmtpprot()))
{
transport.connect(getSmtphost(), Integer.parseInt(getSmtpprot()), username, password);
} else
{
transport.connect(getSmtphost(), username, password);
}
} catch (MessagingException ex)
{
MailingException mailex = null;
Exception nex = ex.getNextException();
if (nex != null)
{
if (nex instanceof SocketTimeoutException)
{
mailex = new SMTPServerException("Connection to Server Failed. SMTP server does not Exist on host--" + getSmtphost() + " -- Mention the proper Host. Other wise Server is very Slow");
mailex.setCause(ex);
}
if (nex instanceof ConnectException)
{
mailex = new SMTPServerException("Connection to Server Failed. Unable to connect to port" + getSmtpprot() + " on SMTP server host---" + getSmtphost() + " -- Mention the proper Server and Port.");
mailex.setCause(ex);
}
}
if (mailex == null)
{
mailex = new SMTPServerException("Connection to Server Failed");
mailex.setCause(ex);
}
throw mailex;
} catch (Exception ex)
{
MailingException mailex = new MailingException("Connection to Server Failed");
mailex.setCause(ex);
throw mailex;
}
return transport;
}
这是我的线程转储的一部分 -
at javax.mail.Service.connect(Service.java:275)
at javax.mail.Service.connect(Service.java:156)
at com.appnetix.app.util.mail.MailSession.getNewTransport(MailSession.java:199)
at com.appnetix.app.util.mail.MailSession.getNewTransport(MailSession.java:249)
locked <0x00000007211b9480> (a com.appnetix.app.util.mail.MailSession)
at com.appnetix.app.util.mail.MailTransport.openConnection(MailTransport.java:122)
at com.appnetix.app.util.mail.MailSenderBase.init(MailSenderBase.java:107)
at com.appnetix.app.util.MailSender.sendMail(MailSender.java:193)
at com.appnetix.app.util.MailSender.sendMail(MailSender.java:175)
答案 0 :(得分:1)
以下是一些提示:
不要在主线程上发送电子邮件。创建一个处理电子邮件的线程池。这很有用,因为通常您不需要等待成功发送电子邮件来回答调用方法。足以说我已经收到你的电子邮件,我会尽快发送(异步)。
您确定需要同步getNewTransport
吗?
尝试重写方法getNewTransport
以返回新的Transport
,而不使用可变实例变量。这样做可以消除synchronized
的使用。