By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,654 Members | 1,519 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,654 IT Pros & Developers. It's quick & easy.

Synchronising threads

P: 4
l have this assignment to do:

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
  1.     try
  2.     {
  3.     Thread.currentThread().sleep(10000);
  4.     }
  5.     catch(InterruptedException e) {}
I've been strugling with this thing for weeks now, I just don't get Synchronization .l just want some advice, a push in the right direction etc.. This is the code that I have to Synchronize:


Expand|Select|Wrap|Line Numbers
  1. /*
  2.  Chapter 12 : Implementing session Tracking 
  3.  Programmer : Abraham Nkomo
  4.  Date       : 21-02-2012
  5.  Filename   : SessionBank.java
  6.  Purpose    : This servlet use session tracking to store 
  7.                the bslsnce as a session attribute
  8.  */
  9.  
  10. import javax.servlet.*;
  11. import javax.servlet.http.*;
  12. import java.io.*;
  13. import java.text.DecimalFormat;
  14.  
  15. public class SessionBank extends HttpServlet
  16. {
  17.     // Create Class Variable
  18.  
  19.     DecimalFormat myFormat = new DecimalFormat("$#,000.00");
  20.  
  21.     public void doGet(HttpServletRequest req, HttpServletResponse res)
  22.             throws ServletException, IOException 
  23.     {
  24.         doPost(req, res);
  25.     }
  26.  
  27.     public void doPost(HttpServletRequest req, HttpServletResponse res)
  28.             throws ServletException, IOException
  29.  
  30.     {
  31.         //Makes instance variable a local variable
  32.         double accBalance = 500.00;
  33.  
  34.         //Set MIME type of content returned to browser
  35.         res.setContentType("text/html");
  36.         PrintWriter out = res.getWriter();
  37.  
  38.         //Starts outputting the HTML Form
  39.         out.println("<html><head>"
  40.                 + "<title>Online Bank Simulator</title>"
  41.                 + "</head><body>"
  42.                 + "<hr color=\"#808000\">"
  43.                 + "<center><h1>Banking Simulation</h1></center>"
  44.                 + "<form method=\"POST\" action=\"../servlet/SessionBank\">"
  45.                 + "<center>Amount: <input type=\"text\" name=\"Amount\" size=\"20\"></center>");
  46.  
  47.         //Decides which action to take
  48.         String action = req.getParameter("act");
  49.  
  50.         if (action != null) {
  51.             if (action.equals("Deposit"))
  52.             {
  53.                 try
  54.                 {
  55.                 double amount;
  56.                 String strAmount = req.getParameter("Amount");
  57.                 amount = Double.parseDouble(strAmount);
  58.  
  59.                 if (amount <= 0.00)
  60.                 {
  61.                     out.println("<h2>Error: The amount is either null or a minus</h2>");
  62.                 } else 
  63.                 {
  64.                     accBalance = accBalance + amount;
  65.                     out.println("<br><center>Balance:" + myFormat.format(accBalance) + " </center> <br>");
  66.                 }
  67.                 }
  68.                             catch(NumberFormatException e)
  69.                             {
  70.                              out.println("<font color=\"red\"><h2>Error :The amount entered is either null or invalid</h2></font>");   
  71.                             }
  72.             }
  73.             else if (action.equals("Withdraw"))
  74.             {
  75.                try
  76.  
  77.                     {
  78.                 double amount;
  79.                 String strAmount = req.getParameter("Amount");
  80.                 amount = Double.parseDouble(strAmount);
  81.  
  82.                 if (amount <= 0.00)
  83.                 {
  84.  
  85.  
  86.                     out.println("<h2>Error: The amount is either null or a minus</h2>");
  87.                 }
  88.                 else 
  89.                 {
  90.                     accBalance = accBalance - amount;
  91.                     out.println("<br><center>Balance:" + myFormat.format(accBalance) + " </center> <br>");
  92.                 }
  93.                 }
  94.  
  95.                             catch(NumberFormatException e)
  96.                             {
  97.                              out.println("<font color=\"red\"><h2>Error :The amount entered is either null or invalid</h2></font>");   
  98.                             }
  99.  
  100.  
  101.             }
  102.             else if (action.equals("Balance")) 
  103.             {
  104.                 out.println("<br><center>Balance:" + myFormat.format(accBalance) + " </center> <br>");
  105.             } else
  106.             {
  107.                 out.println("<br><center>Balance:" + myFormat.format(accBalance) + " </center> <br>");
  108.             }
  109.         }
  110.         else 
  111.         {
  112.             out.println("<br><center>Balance:" + myFormat.format(accBalance) + " </center> <br>");
  113.         }
  114.  
  115.         //Get the current session and set Attribute
  116.         HttpSession session = req.getSession(true);
  117.         session.setAttribute("accBalance", new Double(accBalance));
  118.  
  119.         //Outputs rest of HTML
  120.         out.println("<table width=\"35%\" align=\"center\">"
  121.                 + "<tr><td width=\"33%\" align=\"center\">"
  122.                 + "<input type=\"submit\" name=\"act\" value=\"Deposit\">"
  123.                 + "</td>"
  124.                 + "<td width=\"33%\" align=\"center\">"
  125.                 + "<input type=\"submit\" name=\"act\" value=\"Withdraw\">"
  126.                 + "</td>"
  127.                 + "<td width=\"33%\" align=\"center\">"
  128.                 + "<input type=\"submit\" name=\"act\" value=\"Balance\">"
  129.                 + "</td></tr>"
  130.                 + "</table><br>"
  131.                 + "</form>"
  132.                 + "<hr color=\"#80800\">"
  133.                 + "</body></html>");
  134.  
  135.     }
  136. }
Feb 21 '12 #1
Share this Question
Share on Google+
1 Reply


10K+
P: 13,264
Start by pulling out all the wihtdraw logic into a separate method (class even) that is called from your servlet's doPost. The doPost should just retrieve parameters from various context objects and call classes that do the business logic. Also, why are you creating HTML inside the servlet as was done in 1999? Use JSPs or (better) facelets to separate view logic from controller logic.
Feb 22 '12 #2

Post your reply

Sign in to post your reply or Sign up for a free account.