• Post Reply Bookmark Topic Watch Topic
  • New Topic

Synchronizing  RSS feed

 
Anita Lahav
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not a method, it's a synchronized block that synchronizes on the object that the variable act refers to.

Have a look at Intrinsic Locks and Synchronization, in which this syntax is explained.
 
Anita Lahav
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My apologies, synchronizing block you right not method. I am struggling with synchronizing and have been reading up on it for days now. I got this far and not sure if I have done it correctly.
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using act as the Object to hold the monitor rather than “this”?
 
Anita Lahav
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Testing in two browsers won't be enough - you will need to test in heavy load, many threads accessing the data at one time. For that, you would probably be best running the test in a testing framework rather than an actual server environment.

I think it is okay - even encouraged, to synchronize on something other than this. Since it is the account whose data you want to protect, synchronizing on the account instance makes sure the same data is protected no matter what class/instance/method/thread is accessing it.

I do not like storing any data at all in the servlet instance. You do not control the life cycle of the Servlet, the container can make them and destroy them, you may even have multiple instances of the same servlet in play at the same time. You are better off storing the data in a scope that is more consistent: session for user data, request for data that needs to exist for the life of a request, or application for things that last longer.

You have one Account object per instance of the Servlet. Is that what you want? Do all users and visitors of your site share the same account?

Although your account is protected by the synchronized block, the entered variable is not. That means at any given time the entered variable could be in a bad state, and since your work on account depends on the work on entered, your account data could get inconsistent as well.

I am not sure why entered needs to be an instance variable. It comes in from the request, probably only lives as long as the request, and 'belongs' to an individual. Make it method - local or put it in the request scope if doGet() needs it.

You synchronize way too much. Your entire doGet() processing is in the synchronized block. On a busy system your server would come to a crawl. I would suggest that you limit the size of the block that is synchronized. For example, just copy the values from the servlet instance into method local variables, do the math on them, copy them back to the appropriate scope. Then end the synchronized block, after which you call the doGet() method.

we have to include the HTML code in the servlet

I don't think you ever have to, nor should you, put HTML in a servlet.
 
Anita Lahav
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is an assignment. It reads:

Write a Java servlet that creates an HTML form to synchronize access to the instance variable, balance. Because balance is a double and not an object, it cannot be used as the monitor. Use synchronized methods or synchronized blocks of code, as appropriate. Simultaneously test two threads by starting another instance of your Web browser and loading the servlet n it as well. Explain how the displayed balance verifies that access to the variable, balance, is synchronized.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahh, an assignment, that is different. Paragraphs 5,6,7 are still important. Entered is probably not needed as an instance variable, but if you decide to use it, it must be protected by the synchronized block as well. And you should still endeavor to synchronize exactly as much as you need: no more and no less.

You can ignore the below rant ;)

As far as assignments go, this one is poor. Before you learn about synchronization you should learn about proper scoping, it is a much more common problem, creates more issues, is easier to solve and resolves to best practices. Making an assignment that directs you to create poorly conceived variable scopes is a bad idea. It isn't like you don't need synchronization when you properly scope the data: you do.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anita Lahav wrote:Is this a valid Synchronized method in line 26? Please note that I took some code out...

But what you've written is still hard to read.

Please DontWriteLongLines. It makes your thread very hard to read, and it's actually bad coding practice.
I've broken yours up this time, but for future reference, please remember:
80 characters max.
(the SSCCE page actually recommends 62)
And that includes string literals AND comments AND long method calls.

Oh, and you're usually better off using spaces for indentation, rather than TABs.

Thanks.

Winston
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!