8 Replies - 577 Views - Last Post: 06 October 2013 - 01:44 PM

#1 Anitush  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 29-October 12

Synchronizing

Posted 03 October 2013 - 11:55 PM

Is this a valid Synchronized method in line 26? Please note that I took some code out and that we have to include the HTML code in the servlet.

public class SyncBank extends HttpServlet
{
	private Account act;
	public DecimalFormat pattern = new DecimalFormat("$#0,000.00");	// To set a pattern for display
	public Double actBalance = 0.00;                      		   	// initial display value
	public String displayBalance = pattern.format(actBalance);
	double entered = 0.0;

	public void init(ServletConfig conf)throws ServletException
	{
		super.init(conf);
		act = new Account();
	}

	public void doPost(HttpServletRequest req, HttpServletResponse res)
	throws ServletException, IOException
	{
		try
		{
			entered = Double.parseDouble(req.getParameter("ENTRY"));
		}
		catch(NumberFormatException e)
		{
			entered = 0.0;
		}
		synchronized(act)
		{
			if((req.getParameter("DEPOSIT")!=null) && (entered > 0))
			{
				actBalance = actBalance + entered;
				doGet(req, res);

				try
				{
					Thread.currentThread().sleep(10000);					//sleep for milliseconds
				}
				catch(InterruptedException e) {}
			}
			else if((req.getParameter("WITHDRAW")!=null) && (entered < actBalance) && (entered > 0))
			{
				actBalance = actBalance - entered;
				doGet(req, res);
			}
			else if(req.getParameter("BALANCE")!=null)
			{
				doGet(req, res);
			}
			else
			{
				res.setContentType("text/html");
				PrintWriter out = res.getWriter();
				res.setHeader("Expires","Tues, 01 Jan 1980 00:00:00 GMT");

				out.println("<HTML>");
				out.println("<HEAD>");
				out.println("<TITLE>Incorrect Input Warning..</TITLE>");
				out.println("</HEAD>");
				out.println("<BODY>");
				out.println("<H1>Invalid Entry</H1>");
				out.println("<H2>You have entered an incorrect amount!!!</H2>");
				out.println("<H3>You must enter a number only and it must be greater than 0.0</H3>");
				out.println("</from>");
				out.println("</BODY>");
				out.println("</HTML>");
			}
		}



Is This A Good Question/Topic? 0
  • +

Replies To: Synchronizing

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 823
  • View blog
  • Posts: 2,533
  • Joined: 29-July 11

Re: Synchronizing

Posted 04 October 2013 - 12:01 AM

It looks syntactically valid, unless I missed something...

The method itself is not synchronized, but it has a synchronized block of code.

This post has been edited by farrell2k: 04 October 2013 - 12:02 AM

Was This Post Helpful? 0
  • +
  • -

#3 Anitush  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 29-October 12

Re: Synchronizing

Posted 04 October 2013 - 12:05 AM

Can you maybe explain in more detail? I have to be honest I am struggling with Synchronizing.
Was This Post Helpful? 0
  • +
  • -

#4 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 823
  • View blog
  • Posts: 2,533
  • Joined: 29-July 11

Re: Synchronizing

Posted 04 October 2013 - 02:15 AM

Sure. The Synchronized block is owned by the act object, and everything in it can only be executed by one thread at a time. That's what synchronizing does. As far as what the code does, it appears to add or remove a sum from an account balance.
Was This Post Helpful? 0
  • +
  • -

#5 Anitush  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 29-October 12

Re: Synchronizing

Posted 04 October 2013 - 03:01 AM

Please excuse me but I am still learning. I am not allowed to use balance as this is a double and not an object, it cannot be used as the monitor. So I created act (Private Account act) as the object. I am not sure if I have to synchronize each "button" (Deposit, Withdrawal and balance) on its own.

I must verify that the balance is synchronized by testing it in two web browsers.

Did I did that?I know this is a bad question?
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Synchronizing

Posted 04 October 2013 - 09:47 PM

kind of completly useless to

synchronized(act)

if you do not access act within the block
Was This Post Helpful? 0
  • +
  • -

#7 Anitush  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 29-October 12

Re: Synchronizing

Posted 04 October 2013 - 09:54 PM

How would you suggest I do this? Any help is appreciated.
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10447
  • View blog
  • Posts: 38,690
  • Joined: 27-December 08

Re: Synchronizing

Posted 04 October 2013 - 10:10 PM

Moved to Java EE. Please remember to post topics related to Servlets, Struts, Hibernate, Spring, JSP, JSF, etc., in the Java EE forum.
Was This Post Helpful? 0
  • +
  • -

#9 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 823
  • View blog
  • Posts: 2,533
  • Joined: 29-July 11

Re: Synchronizing

Posted 06 October 2013 - 01:44 PM

View Postpbl, on 05 October 2013 - 04:47 AM, said:

kind of completly useless to

synchronized(act)

if you do not access act within the block


Give him a break. :) It's no different than calling synchronized(this). act is just the owner. It doesn't need to be accessed.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1