5 Replies - 378 Views - Last Post: 02 October 2017 - 05:28 AM

#1 Yevgeni Duvenage  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 11-September 17

Synchronizing on servlet instance variable

Posted 22 September 2017 - 07:43 AM

I am having trouble making a servlet thread-safe using synchronization.

I do know it's best to avoid instance (state) variables in servlets, but my college assignment calls strictly for this.

My servlet has an instance variable, shared among any number of threads that run. I need to use synchronization such that modification of this variable is only allowed to one thread at a time. I then need to test the effects of synchronization employed by using a Thread.currentThread().sleep(10000) delay timer within the synchronized(){} block.

- The instance variable is of type double, so a number.
- I hence can't use it as monitor, as it's not an object.
- I hence set up synchronization blocks around code lines that modify this variable.
- The syntax I used for these synchronization blocks is as follows:

synchronized(this)
    {
    // Actions to perform with the instance variable.
    }


I researched a lot about servlets in this regard. My findings are the following:

- using the "this" word as synchronization object does not work, because "this" is its own "this" for each thread of the servlet running. So if 4 users have simultaneously the servlet open in their browsers and are using it, that means there are 4 instances of the servlet running and using "this", each instance simply refers back to itself -> meaning it is as if there is no synchronization condition anyway. For synchronization to work, all instances must lock on one single variable.
- Another finding states that there is always only one instance of a servlet running, and the multiple simultaneous users means multiple threads. But there is only one instance of the servlet.

This is conflicting information.

Please help: what should I do, how should I code, to synchronize operations using this instance variable, such that each next user (thread) must wait for the first thread to release the lock, before it can perform the operation???

There were suggestions as follows:

1) Construct a new object -> Object lock = new Object();
2) Use synchronization on the desired instance variable, using the object as a "dummy" reference -> synchronize(lock){// Code with variable}.

This still did not work. Currently the servlet behaves as if there are no synchronized blocks at all. Help is hugely appreciated!

Is This A Good Question/Topic? 0
  • +

Replies To: Synchronizing on servlet instance variable

#2 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 644
  • View blog
  • Posts: 4,844
  • Joined: 25-December 13

Re: Synchronizing on servlet instance variable

Posted 23 September 2017 - 10:48 AM

Quote

instance (state) variables in servlets, but my college assignment calls strictly for this

Can you post the part of the assignment that calls for that feature?

I don't think state can be preserved in an instance of a servlet. That is what session is for.
Was This Post Helpful? 0
  • +
  • -

#3 Yevgeni Duvenage  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 11-September 17

Re: Synchronizing on servlet instance variable

Posted 25 September 2017 - 02:22 AM

Hey, NormR, the below are the assignment parts in question:

This next quoted paragraph is of the initial task where I created the servlet from scratch and as you see they indicate I need to use an instance variable, and then test for myself how the fact that it is shared among multiple threads causes it to change in value for all threads using it. I don't know how to upload a screenshot from the HDD to here, hence the typed quoted text:

"4 Creating HTML forms in Java

Write a Java servlet, HTMLBank.java, that creates the HTML form as shown in Figure 12-94. Use an instance variable for the balance. If the user enters a valid amount in the Amount text box and then clicks the Deposit button, add the amount to the balance; if the user clicks the Withdraw button, subtract the amount from the balance. If no amount was entered, or if the amount is negative or zero, output a simple HTML page that displays an appropriate error message. Compile the servlet and install it in the default servlet folder. Start Tomcat. Start your Web browser, type http://localhost:8080/servlet/HTMLBank in the address text box, and then press the Enter key. To execute a second thread, start another instance of your Web browser and load the servlet in it, as well. Switching between the two instances, perform transactions with each. Recall that an instance variable is shared among multiple threads. Write a short paragraph that explains how the displayed balance verifies that the variable, balance, is shared. Hint: recall the use of a DecimalFormat to format numeric output. Use the static method, Double.parseDouble(StringVariable), to convert the String from the text box parameter, amount, to a double. Be sure that an amount was entered before converting to a double".

And the next is a representation of the expected look of the servlet page:

Bank ATM Simulation
_______________
Amount: | | |
|_______________|

Balance: $3,000.00
_________ _________ __________
| Balance | | Deposit | | Withdraw |
|_________| |_________| |__________|

The above I have successfully completed.

Now the next quoted text is what I am trying to do now, and it is based on the above -> a modification to it:

"5 Synchronizing Threads

Modify the program in no.4 to synchronize access to the instance variable, balance. Save the program as SyncBank.java. Because balance is a double and not an object, in cannot be used as the monitor. Use synchronized methods or synchronized blocks of code, as appropriate. Simultaneously test two threads as was done in no.4. 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:

// Delay a deposit for a visible amount of time
try
{
Thread.currentThread().sleep(10000); // sleep for milliseconds
}
catch(InterruptedException e)
{
}

Write a short paragraph that explains how the displayed balance verifies that the access to the variable, balance, is synchronized.".

So these are their instructions.
Was This Post Helpful? 0
  • +
  • -

#4 Yevgeni Duvenage  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 11-September 17

Re: Synchronizing on servlet instance variable

Posted 27 September 2017 - 01:39 AM

The next task I am to do indeed instructs to remove instance variables and use session attributes, with which no synchronization is needed and multiple threads will not interfere with one another.

But the current task in discussion instructs me to synchronize access to this instance variable. Is there then really no way to achieve this?
Was This Post Helpful? 0
  • +
  • -

#5 Yevgeni Duvenage  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 11-September 17

Re: Synchronizing on servlet instance variable

Posted 02 October 2017 - 03:15 AM

Any ideas? Thanks.
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • lashings of ginger beer
  • member icon

Reputation: 6334
  • View blog
  • Posts: 25,541
  • Joined: 12-December 12

Re: Synchronizing on servlet instance variable

Posted 02 October 2017 - 05:28 AM

Quote

Modify the program in no.4 to synchronize access to the instance variable, balance. Save the program as SyncBank.java. Because balance is a double and not an object, in cannot be used as the monitor. Use synchronized methods or synchronized blocks of code, as appropriate.

Have you pursued the first (bold) suggestion, synchronised methods? A quick search led me here.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1