Synchronize access to the instance variable, accBalance. Because accBalance is a double and not an object, it cannot be used as the monitor. Use synchronized methods or synchronized blocks of code, as appropriate. Simultaniously test two threads. Because the threads can complete too quickly to determine if they are interfering with each other, delay the adding of a deposit by inserting the following code within the synchronized block or method:
Expand|Select|Wrap|Line Numbers
- try
- {
- Thread.currentThread().sleep(10000);
- }
- catch(InterruptedException e) {}
Expand|Select|Wrap|Line Numbers
- /*
- Chapter 12 : Implementing session Tracking
- Programmer : Abraham Nkomo
- Date : 21-02-2012
- Filename : SessionBank.java
- Purpose : This servlet use session tracking to store
- the bslsnce as a session attribute
- */
- import javax.servlet.*;
- import javax.servlet.http.*;
- import java.io.*;
- import java.text.DecimalFormat;
- public class SessionBank extends HttpServlet
- {
- // Create Class Variable
- DecimalFormat myFormat = new DecimalFormat("$#,000.00");
- public void doGet(HttpServletRequest req, HttpServletResponse res)
- throws ServletException, IOException
- {
- doPost(req, res);
- }
- public void doPost(HttpServletRequest req, HttpServletResponse res)
- throws ServletException, IOException
- {
- //Makes instance variable a local variable
- double accBalance = 500.00;
- //Set MIME type of content returned to browser
- res.setContentType("text/html");
- PrintWriter out = res.getWriter();
- //Starts outputting the HTML Form
- out.println("<html><head>"
- + "<title>Online Bank Simulator</title>"
- + "</head><body>"
- + "<hr color=\"#808000\">"
- + "<center><h1>Banking Simulation</h1></center>"
- + "<form method=\"POST\" action=\"../servlet/SessionBank\">"
- + "<center>Amount: <input type=\"text\" name=\"Amount\" size=\"20\"></center>");
- //Decides which action to take
- String action = req.getParameter("act");
- if (action != null) {
- if (action.equals("Deposit"))
- {
- try
- {
- double amount;
- String strAmount = req.getParameter("Amount");
- amount = Double.parseDouble(strAmount);
- if (amount <= 0.00)
- {
- out.println("<h2>Error: The amount is either null or a minus</h2>");
- } else
- {
- accBalance = accBalance + amount;
- out.println("<br><center>Balance:" + myFormat.format(accBalance) + " </center> <br>");
- }
- }
- catch(NumberFormatException e)
- {
- out.println("<font color=\"red\"><h2>Error :The amount entered is either null or invalid</h2></font>");
- }
- }
- else if (action.equals("Withdraw"))
- {
- try
- {
- double amount;
- String strAmount = req.getParameter("Amount");
- amount = Double.parseDouble(strAmount);
- if (amount <= 0.00)
- {
- out.println("<h2>Error: The amount is either null or a minus</h2>");
- }
- else
- {
- accBalance = accBalance - amount;
- out.println("<br><center>Balance:" + myFormat.format(accBalance) + " </center> <br>");
- }
- }
- catch(NumberFormatException e)
- {
- out.println("<font color=\"red\"><h2>Error :The amount entered is either null or invalid</h2></font>");
- }
- }
- else if (action.equals("Balance"))
- {
- out.println("<br><center>Balance:" + myFormat.format(accBalance) + " </center> <br>");
- } else
- {
- out.println("<br><center>Balance:" + myFormat.format(accBalance) + " </center> <br>");
- }
- }
- else
- {
- out.println("<br><center>Balance:" + myFormat.format(accBalance) + " </center> <br>");
- }
- //Get the current session and set Attribute
- HttpSession session = req.getSession(true);
- session.setAttribute("accBalance", new Double(accBalance));
- //Outputs rest of HTML
- out.println("<table width=\"35%\" align=\"center\">"
- + "<tr><td width=\"33%\" align=\"center\">"
- + "<input type=\"submit\" name=\"act\" value=\"Deposit\">"
- + "</td>"
- + "<td width=\"33%\" align=\"center\">"
- + "<input type=\"submit\" name=\"act\" value=\"Withdraw\">"
- + "</td>"
- + "<td width=\"33%\" align=\"center\">"
- + "<input type=\"submit\" name=\"act\" value=\"Balance\">"
- + "</td></tr>"
- + "</table><br>"
- + "</form>"
- + "<hr color=\"#80800\">"
- + "</body></html>");
- }
- }