Hi to everyone

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:

try
{
Thread.currentThread().sleep(1000);
}
catch(InterruptedException e
{
}

I just don't get Synchronization. I've tried reading up on it but it just doesn't help me .This is the code that I have to Synchronize:

import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    import java.text.DecimalFormat;
     
    public class SyncBank extends HttpServlet
    {
    // Create Class Variable
    DecimalFormat myFormat = new DecimalFormat("$#,000.00");
    double accBalance = 0.00;
     
    public void doGet(HttpServletRequest req,HttpServletResponse res)
    throws ServletException, IOException
    {
    doPost(req,res);
    }
     
    public void doPost(HttpServletRequest req,HttpServletResponse res)
    throws ServletException, IOException
    {
     
    //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/HTMLBank\">" +
    "<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"))
    {
    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>");
    }
    }
    else if(action.equals("Withdraw"))
    {
    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>");
    }
    }
    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>");
    }
     
     
    //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>");
     
    }
    }

What tutorials have you read?
Go read the API doc for the wait and notify methods of the Object class. There is a discussion of Threads and monitors there.

For learning how to use synchronized and threads, you should write a simple program to use those features.
The code you posted has too much code that is NOT related to the problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.