473,383 Members | 1,801 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Synchronising threads

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
1 1917
r035198x
13,262 8TB
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

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

Similar topics

2
by: justme | last post by:
Dears I have created the following page to display my data <head> <style type="text/css"> body, td, th, h1, h2 {font-family: sans-serif;} body, td, th {font-size: 100%;} a:link {...
6
by: paulus4605 | last post by:
Dears I have the following problem I’m using a query to get all the data from my database from the past year the second query is displaying the results by month. How can I match the second...
2
by: fishfry | last post by:
I'm learning about threads and I noticed there are two different ways I can implement a runnable object. In one way, I have one object and two threads; in the other way, I have two objects and two...
17
by: Andrae Muys | last post by:
Found myself needing serialised access to a shared generator from multiple threads. Came up with the following def serialise(gen): lock = threading.Lock() while 1: lock.acquire() try: next...
1
by: judy | last post by:
I have a number of Acess databases on a web site and copies of these databases on my local PC. The databases can be independently updated on the web and also on my local PC. Is there some way...
1
by: Michael Thomas | last post by:
Hi everyone Having a slight problem synchronising tables in MS Access 2002. I have two tables: Products (StockCode*, Description, CostPrice, SellingPrice) and
1
by: Rajesh Sharma | last post by:
Dear all, please help me in Combo Box Synchronising. I have three levels. Category -> Type -> Subtype Depending on the selected Category, the Type appears in the second combo box and...
2
by: Joel Moore | last post by:
I'm looking at my debugger right now and can clearly see that two threads have their execution pointer residing within the same Synclock block. One thread is executing a Monitor.Wait(). I know...
2
by: Matthew Tylee Atkinson | last post by:
I appear to be having some problems with the isAlive() method of detecting if a thread is alive/active/running or not. I'd be grateful for any advice. I have a visualisation program (which uses...
3
by: chromis | last post by:
Hi, I've just setup a development server for testing websites at my office and wish to synchronise it with the sites folder on our live server, does anyone know of any good programs for this? ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.