This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

synchronization  RSS feed

 
Shivangi Joshi
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hiiiiiii,
i am using synchronized object in an application. if i am accessing same application on 2 different machines ,then still that object behave in synchronized way? or its synchronization property is lost?

thanks in advance
shivangi...
 
Nishan Patel
Ranch Hand
Posts: 689
Eclipse IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

That is depending upon your object type. In J2EE only local variable and request object are thread safe. So if you synchronized it will synchronized if you are accessing from any number of computers.... And if you are accessing at simple java core java then also synchronized object works fine..
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you accessing the same object from two different apps? That may make a difference in how you implement it.
 
Campbell Ritchie
Sheriff
Posts: 55315
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Too difficult a question for us beginners. Moving.
 
Shivangi Joshi
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello ,

what i exactly want to say is, if i have an application which have one field which is autoincremented, like as n when user add the record, the number will get incremented automatically.. now my problem is when 2 or more user trying to add record , from different machin. all of them are getting same number by saving the record. so the next process goes on toss.. i want something which will restrict record from saving when such condition occure. synchronization is not work in this case. so what could be its solution. whether a kind of class lock will be my solution. or something else.?

thanks in advance
shivangi...
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As was asked before, what is your environment. Are you running a web application? Or an application on the users machine?

If you have a web application:
1) Synchronization SHOULD work in this case. If it is not, then you are doing it incorrectly. Showing us some code and telling us what "synchronization is not work" specifically means in this case may help us fix your current code.

2) A better, Java-based option for this would be to use an AtomicInteger or AtomicLong to hold the static, auto-incrementing value. When a new record is added it would use the incrementAndGet() method to get the locally updated value (and store it locally to the record).

3) The best solution may be to let your database do this for you. They do a really good job at this sort of thing. Assuming you are using a database.

Both steps 1 and 2 would need some persistence mechanism to make sure that when the application restarts you don't lose the real count of records. With option 3 the value is persisted for you.


If you have an application that runs on the client's computer, then options 1 and 2 won't work. Both values would be stored per - JVM. You would have to perform some work to communicate between the different machines. If you choose option 3 then this is done for you.
 
Shivangi Joshi
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Steve,

you can eve try out this because i am not allowed to out the code , so if you can ,try out this . one program having field count which auto increment when say, you click a button (assuming that you got scenario .. like a screen which is having a textfield dispalys current count value and a button . On actionEvent of this button increments the count value.) so if you and one more person trying to use 'same program at same time' ,then both of you will get same number, which is wrong. it should not allow same number .

so i hope you got exact situation.....

thanks
shivangi....
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shivangi Joshi wrote:Hello Steve,

you can eve try out this because i am not allowed to out the code , so if you can ,try out this . one program having field count which auto increment when say, you click a button (assuming that you got scenario .. like a screen which is having a textfield dispalys current count value and a button . On actionEvent of this button increments the count value.) so if you and one more person trying to use 'same program at same time' ,then both of you will get same number, which is wrong. it should not allow same number .

so i hope you got exact situation.....

thanks
shivangi....


What does 'same program at the same time' mean? There are too many possibilities and I can't duplicate your scenario. Perhaps try to make a SSCCE that demonstrates the problem.

But I guess the question comes down to: Are you using a database? If yes: Use an auto-increment field in the DB to generate the counter, regardless of the application type. If no: the situation depends entirely on how your application works.
 
Ajay Saxena
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I put all of your posts together and read,I get the following picture.

You have a (possibly web) server based application that's being concurrently accessed by multiple users,each from a different client machine.And every time,a user hits the server placing a request for a record creation (by hitting some button on the client GUI),you want some count variable to be incremented on the server side.d

This is a very simple and extremely common problem that can be solved in several ways,as described by Steve.

The need to synchronize the record count across multiple client machines is a bit outlandish. A typical web app would be having 1000s of users and hence 1000s of client machines.You would not want to sync the record count across 1000s machines.After all that's the job of the server.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!