473,385 Members | 1,782 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,385 software developers and data experts.

Does this case need to be thread safe in Java ?

i have create a functionality such that there are two boxes left and right. The values will be checked in the left box and after clicking on right arrow (>>) the value will get moved into the Right box. Vice versa with left arrow (<<).

I am displaying these values using Java HashMap . My question is if two different users login the site and they move the values from one box to another , ( they are seeing the same values ) than this type of scenerios need to be thread safe ( synchronized ) ? Or it will work without synchronized also ?

By every click on arrow i am calling my java class by jquery get request and updating the Java Map in java class and getting back the response via jquery and displaying the updated values in both the boxes again.
Apr 5 '14 #1
3 1350
Nepomuk
3,112 Expert 2GB
If the HashMap is shared between the users then yes, it should be synchronized two instances may be trying to access the HashMap at the same time, causing unforeseen behavior.
Apr 7 '14 #2
chaarmann
785 Expert 512MB
There is no way that you can synchronize two browser windows.
Even if you synchronize the map on the server, you cannot get rid of (or synchronize) the delay between the server sending the update and the client browser showing the update.

Better to use locking mechanism here: When the first user opens the page, he can modify entries in the boxes, but for all other users calling this page the boxes are shown, but locked until the first user finishes (or times out).

Fazit:
If you use locking mechanism on browser level (as a type of synchronization), then you don't need to synchronize on java level (the HashMap)

Please take in account that if 2 user open the same page, the first user can send his inquiry first, but the inquiry from the second user can arrive earlier at the server (different routing), so don't do anything client-browser-time based. Always use server time.

What I mean is demonstrated in this example:
1.) First user opens page, he can edit for 60 seconds.
2.) second user opens page, it is locked for 60 second.
3.) first user moves entry after 59 seconds. This query 1 is on its way to the server and will take 5 seconds.
4.) Because 60 seconds passed (javascript browser trigger), the page of second user is unlocked. He moves the same entry immediately (after 2 seconds). This query 2 is on its way and will take 1 second.
5.) Query 2 arrives before query 1 and will be processed successfully.
6.) Query 1 will be processed and user 1 gets an error, because the entry to be moved cannot be found in first table.
Apr 7 '14 #3
Nepomuk
3,112 Expert 2GB
Well, that depends on what the intention is. If you want to optimize for several people being able to modify at the same time you will have to have some kind of conflict handling. Take the conflict management in SVN as an example here - if you try to commit changes and someone has already changed it since you last updated your version there will be a merge conflict which must be resolved.
If however you want two users to be shown the same stuff at all time, you can use a locking mechanism as described by chaarmann.
Apr 7 '14 #4

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

Similar topics

5
by: nicolas_riesch | last post by:
Does someone know if the module pytz (http://sourceforge.net/projects/pytz/) is thread-safe ? I have not seen it explicitely stated, and just wanted to be sure, as I want to use it. That's...
8
by: Senna | last post by:
Hi I have some questions about this code, http://code.doria.se/?c=18. 1. Will the Adapter class be thread-safe? 2. In the #region Way 1 I use lock(_lockobj) every time I go about and do...
15
by: Laser Lu | last post by:
I was often noted by Thread Safety declarations when I was reading .NET Framework Class Library documents in MSDN. The declaration is usually described as 'Any public static (Shared in Visual...
1
by: jecheney | last post by:
Hi, Im currently using the following code for reading/writing to a network socket. private StreamReader clientStreamReader; private StreamWriter clientStreamWriter; .... TcpClient tcpClient...
7
by: reyesflaco | last post by:
I am developing an application using asp.net 2.0. I created all my business objects in my app_code folder. As of right now, all my classes are public. In my aspx pages, I am declaring the class...
8
by: =?Utf-8?B?T2xpdmVy?= | last post by:
Hey there, im quite new to C# GUI-Framework and got a question now. In Java its no problem todo cross-over Thread-calls. So i can change something in a Window from another thread. It seems C#...
44
by: climber.cui | last post by:
Hi all, Does anyone have experience on the thread-safty issue with malloc()? Some people said this function provided in stdlib.h is not thread- safe, but someone said it is thread safe. Is it...
3
by: =?Utf-8?B?anBhdHJjaWs=?= | last post by:
Don't see any official notice that compiled library dll's loaded in the BIN directory of an asp.net website need to be thread safe, but concurrent visits to the same web site sure bear this out....
3
by: andreas.zetterstrom | last post by:
I'm implementing some different c++ classes which I want to be thread safe. All these classes contain lists or arrays of some kind. I'm using protected sections to make them thread safe. The...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.