How to synchronize a method for all instances of a class
Hi,
How to make a method synchronized for all instances of a class? If a simple method is synchronized, then multiple threads cannot access it at the same time. If we make the method as static, then we are making it synchronized at class level.How to make a synchronized method so that no two instances (objects) of a class can access it at the same?
Thanks
Neha
Neha_Khands wrote:
There is nothing wrong with that. Actually this question was asked in an interview. They didnt want to create a static method. They told me that synchronization can be achieved at instance level also. and for that we have to call some Class.getInstance().synchronied method inside constructor. Kind of a dumb question. First, synchronization does not occur "at a class level" or "at an instance level." Syncing is always the same--a single object's lock is obtained, which prevents any other threads from obtaining that lock. The only thing that makes it appear that there are special cases is that declaring a method synchronized obtains the lock associated with the instance or with the Class object for that class. But that's just syntactic sugar. The Class object lock is identical to the instance lock, which in turn is identical to a lock on some other arbitrary Object created just to serve as a lock. There's no such things as "locking the class" or "locking the instance."
Second, and more important, making an instance method synced across all instances is a grotesquely artificial situation, IMHO, and if it were to ever come up, the right way to do it is to have that instance method call a static synchronized method.
Similar Messages
-
How can I call a method for all instances of a certain class?
Hi,
I have created a snake java applet, which you can play here [http://users.telenet.be/javagames/|http://users.telenet.be/javagames/].
But now I want to make this into a multi-player game (with two or more snakes etc..).
In my main class you can find methods like these:
snake.move();
snake.drawSnake();Now if I just alter my main class and make a second instance of the class snake and change my methods to this,
snake.move();
snake2.move();
snake.drawSnake();
snake2.drawSnake();my game almost works perfectly, I'll have to make some minor changes though.
But my question is, is there a better way of doing this. Instead of duplicating all the code, how can I make the methods immediately work for both snakes, instead of calling it twice?
All suggestions are welcome!
Thanks in advance.
LennartIf there was something common to all snake instances then you could declare that variable static.
Note: I don't want to confuse you here. A static variable can have a value apart from any instances at all. Instances can view static variables but a static method or inner class, for example, cannot view instance variables.
More generally, I would think you would want to treat each snake as an automaton with its own behavior. There must be some class such as Game or Board or whatever that has, as suggested, either an array or collection of snakes. Iterate over each snake, ask it to perform some computation for the game and then draw itself.
- Saish -
How to Synchronize two methods or more methods
HI All,
As a java starter in learning Synchronization. how simply can I Synchronize two methods
for example I have the following.
public class Process {
public static void main(String args[]){
process1();
process2();
private void process1(){
private void process2(){
}A-KADRI wrote:
HI All,
As a java starter in learning Synchronization. how simply can I Synchronize two methods
for example I have the following.
public class Process {
public static void main(String args[]){
process1();
process2();
private void process1(){
private void process2(){
You're being really ambiguous. You could "synchronize" those methods by adding the synchronized keyword to their method signature, in which case they will implicitly lock on the object they belong to. That's what most people will think you mean when you say synchronized. Is that what you meant? Or did you mean what BDLH was thinking in that you wanted to lock something, then do 1,2,3,4, then unlock it? That's something different. Be specific! -
How to override truncateToFit method for SuperTabNavigator
Hi All,
How to override truncateToFit method for SuperTabNavigator.
we have editableLabel method for changing the tab name.
it is dispalying the ... elipse when entered characters grater than the tab width. it is ok.
but if the entered characters less than the tab width it is also appending the ... elipse.
i dont want that . how to remove those. i dont want completely truncateToFit option.
how to override .
Can any help me regarding this?
Thanks in Advance
Raghu.Give me a sample codeNo. Read the links provided by Yannix, try it out for yourself, and if you still have a question, post the code you tried.
db -
How to query opening balance for all customer or Vendor for an speci. date
Hi,
How to query opening balance for all customer or Vendor for an specific date?
Example:
put any date and query will show all customer/ Vendor that date opening/current balance.
Regards,
MizanHi mizan700 ,
Try this
SELECT T0.[DocNum] As 'Doc No.', T0.[CardCode] As 'Customer Code',
T0.[CardName] As 'Customer Name',(T0.[DocTotal]-T0.[PaidSys]) As 'O/S Balance'
FROM OINV T0 INNER JOIN OCRD T1 ON T0.CardCode = T1.CardCode
INNER JOIN OCRG T2 on T1.GroupCode = T2.GroupCode
INNER JOIN INV1 T3 ON T0.DocEntry = T3.DocEntry
WHERE T0.[DocStatus] ='O'
AND
(T0.[DocDate] >='[%0]' AND T0.[DocDate] <='[%1]')
Regards:
Balaji.S -
Can I set an mx control property globaly for all instances of the control.
Is there a way of setting the value of a variable on a control for all instances of the control in the application?
For example, I have several instances of an mx:Label and I want to change the enabled property for all instances at once rather than refer to each one individually by id.One way is to subclass Button and change its enabled getter to watch some static property. You don’t really want to change Button itself because Buttons exist in lots of places like ScrollBars.
-
HT201304 How do I set password for all in app purchases?
How do I set password for all in app purchases? xx
In-App purchases cannot be made without entering your iTunes store account password.
-
In mdx how to get max date for all employees is it posible shall we use group by in mdx
in mdx how to get max date for all employees is it posible shall we use group by in mdx
example
empno ename date
1 hari 12-01-1982
1 hari 13-06-2000
by using above data i want to get max dataHi Hari3109,
According to your description, you want to get the max date for the employees, right?
In your scenario, do you want to get the max date for all the employees or for each employee? In MDX, we have the Max function to achieve your requirement. You can refer to Naveen's link or the link below to see the details.
http://www.sqldbpros.com/2013/08/get-the-max-date-from-a-cube-using-mdx/
If this is not what you want, please provide us more information about the structure of you cube, so that we can make further analysis.
Regards,
Charlie Liao
TechNet Community Support -
SNR License Issue. How Can I Disable SNR for all Users?
Hi,
I Have an issue with SNR on CUCM 10.5 and with assignement of license type
I have a cluster with 400 users.
200 users use ip phone 3905 and should be use an Essential License.
When system check for assignement of license type , itassign a Basic License instead of an Essential License, because it see that users have SNR Enabled.
All users in my system have SNR enabled, and I can not disable it. All my user have Enable Mobility unchecked, and all my phone have Device Mobility Off, but system however see SNR enable.
How can i disable SNR for all users? This is a feature that client don't need.
I dont' have a sufficent number of licenses for support all 3905 in Basic License.
thanks for help.
AndreaWell actually, Mobile Identity wouldn't apply to a 3905 unless those phones had another line on a Dual-Mode device like an iPhone or Android phone. So unless you have either of those in your system you can scratch that idea. Someone else may chime in here and try to help a bit more as RD/RDP are the main SNR culprits.
-
How to create delivery device for all users ?
hi
help needed ...
i activated delivery tool
but i dont know how to create email devices for all users
is there any api / manual action
obiee version : Oracle Business Intelligence 10.1.3.4.1
thanks
yuvalYuval..u need to setup SA System in RPD..
refer http://oraclebizint.wordpress.com/2008/04/25/oracle-bi-ee-101332-sa-system-subject-area-autoloading-profiles-and-bursting/ -
I want to make a CD that is NOT in alphabetical order, not in artist alphabetical order and not in album alphabetical order... I want to make it the order I want. Can this be done? How? thanks in advance for all your help. Mona
cjam732 wrote:
Ed - by sequence of numbers, are you talking about the rectangle with the arrow that sorts by track number? I've tried to follow your directions to Mona but my playlist still gets burned to CD in alpha order. driving me crazy. any other suggestions? thanks, Chris
Chris, The sequence number is the first column of numbers, to the left of the Name column. It always goes 1, 2,3 etc. It is not the same as the Track Number, which has its own column.
As a precaution before burning, make sure that Shuffle is tunred off, and that you right-click the playlist name and choose 'Copy to Play Order" (although technically they should not affect the burn). -
Please tell me how can I put Bookmarks for all pages opened in Firefox.There was an option ("bookmark all pages") which vanished.
It moved to tab contextual menu :
Right-click on any tab -> Mark all tabs (or something close, my FF is not in english). -
How to install plugin globaly (for all users) in Firefox13?
How to install plugin globaly (for all users) in Firefox13?
See also:
*https://developer.mozilla.org/en/Gecko_Plugin_API_Reference/Plug-in_Development_Overview#Installing_Plug-ins -
Can any one suggest me how to use drawPixels method for 40 series devices
Hello!
I am using drawPixels method of DirectGraphics class the code I have written is :-
Image offscreen=DirectUtils.createImage(width,height,0x00000000);// width and heights are integer value
public final int MODEL=DirectGraphics.TYPE_INT_8888_ARGB ;
Graphics offgra = offscreen.getGraphics();
DirectGraphics directgraphics = DirectUtils.getDirectGraphics(offgra);
directgraphics.drawPixels(imgData,false,0,width,0,0,width,height,0,MODEL); // imgData is a int array(int imgData[]) which contains required pixels of image.
The above code is working fine with NOKIA 60 series device but when i use it with NOKIA 40 series device it gives java.lang.IllegalArgumentException.
same time if i use :-
directgraphics.drawPixels(imgData,false,0,width,0,0,width,height,0,DirectGraphics .TYPE_USHORT_4444_ARGB ) ;
// imgData is a short array(short imgData[]) which contains required pixels of image. i have used different formet here.
it works fine with 40 series device,
can any one suggest me how to use drawPixels method for 40 series devices with format
DirectGraphics .TYPE_INT_8888_ARGB .If Remote wipe is activated it can't be undone. And Once the Wipe is done, the device can nö longer be tracked.
Sorry. -
How to block a website (for all devices) on an EA6900
I'm trying to figure out how to block a website for all devices connected to my router. The only thing I can find is blocking websites by device. Please tell me that they did not take this feature away.
welcome to the 'smart' world. I think one of the best improvements Belkin could do is to give us back the option of the old gui. so much more useful.
Maybe you are looking for
-
How can i build RPMs file for my own Java Application?
How can i build RPMs file for my own Java Application?....I have my own directory that contains all Java Source files and some files that my Application required....I want to build RPMs file like a install File to Linux System, Now my OS is Linux Fed
-
-43 error when import Imovie into Imovie
Hello, I want to import an imovie into another Imovie but I get a -43 error : saying impossible to import the file. Quicktime could not analyse it : -43. I removed plug ins from library/Imovie What I did : open a Imovie project. Dragged a project fro
-
Problem with reusing construction from userInfo(SRDemo) in other project
I want to realize how ADF Faces work, for this i look into SRDemo and the documentation. Now i want to reuse the construction from the bean userInfo in my own project. I have build the bean userInfo and the class ...UserInfo. In my jspx site i use th
-
I need to download my previously purchased movies and TV series. They are not showing up in my "Purchased" folder. Why is that?
-
When I went to restore my Ipod before the message "There was a problem downloading the ipod software for the ipod. The requested resource was not found." came up. I have no idea what to do! IPod Click Wheel Windows XP