in the below code    public class MsLunch {     private long c1 = 0;     private long c2 = 0;     private Object lock1 = new Object();    // private Object lock2 = new Object(); private Dummy lock2 = new Dummy();     public void inc1() {         synchronized(lock1) {             c1++;         }     }      public void inc2() {         synchronized(lock2) {             c2++;         }     } }   i have changed the lock object used to protect the c2 count. i have used a dummy class which is no where related to MsLunch class. Is it fine or should i always put the lock on the object of the current class("this" or MsLunch object or parent class Object). Please answer as i have been  struggling a long way to understand the role of object used in synchronized block. Also is it necessary that the synchronized block member fields and variables must be held by the object used to lock ?
Whenever a question pops up on SO about Java synchronization, some people are very eager to point out that synchronized(this) should be avoided. Instead, they claim, a lock on a private reference is to be preferred.  Some of the given reasons are:    (very popular this one, also has an "accidentally" variant)     Other people, including me, argue that synchronized(this) is an idiom that is used a lot (also in Java libraries), is safe and well understood. It should not be avoided because you have a bug and you don't have a clue of what is going on in your multithreaded program. In other words: if it is applicable, then use it.  I am interested in seeing some real-world examples (no foobar stuff) where avoiding a lock on this is preferable when synchronized(this) would also do the job.  Therefore: should you always avoid synchronized(this) and replace it with a lock on a private reference?      Some further info (updated as answers are given):   we are talking about instance synchronization both implicit (synchronized methods) and explicit form of synchronized(this) are considered if you quote Bloch or other authorities on the subject, don't leave out the parts you don't like (e.g. Effective Java, item on Thread Safety: Typically it is the lock on the instance itself, but there are exceptions.) if you need granularity in your locking other than synchronized(this) provides, then synchronized(this) is not applicable so that's not the issue
Calculate the uncertainty in velocity of electron I got this question in one of my periodic test in coaching, I (seemingly) solved the question but later recognised what mess I did.  So, the question is :      Calculate the uncertainty in velocity of an electron. If the uncertainty in its position is ${10^ {-15}} \; \mathrm m$.   By simple calculations one can see that the answer would be ${5.79\times{10^{10}}} \; \mathrm{m/s}$. However, as one can see, the uncertainty in velocity is greater than the velocity of light then how is that possible or there is some other way to handle such problems.  P.S. I know the concept of dynamic mass but I am not very familiar with it. (I think that would work here, but don't know how?) Prove that if $d$ divides $n$ then $φ(d)$ divide $φ(n)$ for $φ$ denotes Euler’s $φ$-function.  I know that $d|n$ mean there exists some integer $k$ such that $n=kd$, but how can I use this to prove $φ(d)$ divide $φ(n)$ In Trailhead we have the query to get the Contacts for the Account named "SFDC Computing", with an inner query.   SELECT Name, (SELECT LastName FROM Contacts) FROM Account WHERE Name = 'SFDC Computing'   It works fine for my purposes.  "Contacts" is the name of the relationship, per Trailhead. So, I went to Object Manager and examined the Account object's "Fields and Relationships". There is nothing named "Contacts"!   In general, how do I get the name of the relationship, for use in similar SOQL queries? Natbib puts reference data one line per item I'm using:  \documentclass[oneside,article,openbib,12pt,titlepage]{memoir} \usepackage[authoryear,round]{natbib} % Better bibliography support \bibpunct{(}{)}{,}{a}{,}{,}   in the preamble of my document and  \backmatter \renewcommand{\bibname}{References} \bibliographystyle{chicago}    \bibliography{listb,mybib}   in the body of the file. But a typical formatted reference is:     Freely, I. (1997).   A small paper.   The journal of small papers -1. to appear.   I've tried other non-natbib styles (e.g., regstud), and the reference flows and wraps as it should. So how does one get natbib to do the same?  Also, how does one get natbib to include the authors' entire first names? I need to concatenate two String arrays in Java.  void f(String[] first, String[] second) {     String[] both = ??? }   What is the easiest way to do this? Infinite coproduct of abelian groups One can see on every text (book, lesson, comments) that a direct sum/coproduct of abelian groups is the same as a finite product but in the infinite case, the direct sum/coproduct is only a subgroup of the product.  Why does an element in an infinite direct sum only have finitely many components different from the neutral elements?  I don't see where the "finite" comes from when I look at the universal property.  Also, in another situation, a condition of "finiteness" appears and I think that is because of the same structure: a freely generated group, vectors space, algebra etc... has elements that are finite product/sum of generating elements. Because, if one really wants an infinite "sum" (or sequence) one may just take a formal serie. That is a ring, hence also an abelian group. I'm looking for a site, similar to StackOverflow, but with possibility to ask opinion based questions Since opinion based questions are not allowed neither here nor on stackoverflow, I would like to ask if any of you, know some other web page (outside Stack* ecosystem), where we can ask questions like:  "Which Java Web framework would you recommend to learn"   or any other, which by nature are opinion based.  EDIT1:  Allow me to explain a bit more - I know there are webgroups, forums and mailing lists BUT most of those are for single programming language or framework and I'm looking for something more generic. Stackoverflow is great because there are experts from all sort of programming languages and frameworks, so if it would be allowed to ask:   "Best web frameworks similar to ASP.NET MVC4"  I would get a lot of opinions, from different languages and frameworks, and that exactly what I'm looking for. $x^p-c$ has no root in a field $F$ if and only if $x^p-c$ is irreducible? I run a large bulletin board and I get spammers every now and again. My moderation team does a good job filtering them out but every time I IP ban them they seem to come back (I'm pretty sure it's the same person on some occasions, as the post patterns are exactly the same as are the usernames) but I'm afraid to ban them by IP address every time.  If they are on a dynamic IP address, I could be banning innocent users later down the line when they try to get to my forum through SERPs, but if I ban only via static IPs I know that I'm only banning that one person.  So, is there a way to properly determine if an IP address is static or dynamic?  Thanks. Check variable equality against a list of values Why is "letter" not plural in "two letter words"? Why is "letter" not plural in "two letter words"?  For me it's very strange as the equivalent in French would be plural but my English friend finds it totally normal. Opening document library files in popup or new window Is there any easy way to open the document library files in a new window or popup depending on the file types?  e.g. PDF - In Adobe reader (currently opens in IE)  Image file (currently opens in IE) Centering figure captions in IEEEtran I am working on a paper using the IEEEtran template in LyX.  I have run into a problem: While the contents (in this case, a table) of the figure float are centered, the caption is aligned to the left (see screenshot below). How can I center the caption as well?    This is generated by the following LyX document:    What I've tried:   Center the table (the table is centered) Center all the contents in the float (only the table is centered) Select the caption and center it (no effect)   Technical details:   LyX 2.0.5.1 Mac OS X 10.7.5 (Lion) All centering was performed by Right Click>Paragraph Settings>Align: Center Slit screen and wave-particle duality Get driving directions using Google Maps API v2 Is there a way to see who voted on your posts? Can the most important information of reputation audit appear first? I regularly find myself having to scroll to the bottom of the reputation audit to find all the good stuff that appears last. On my computer this is a single key press, but on my mobile it takes 30 seconds of scrolling. Lord alone knows how long it would be for Jon Skeet!  Since the principles of good user experience suggest putting the most important information first, why not do the same here? I never look at my first 5 days of rep gathering. But I often look at the current day, the true rep, days with 200 rep (hunting legendary badge!), trigger recalc etc. Pretty please, can we have it up top? If we have 2 batteries one of emf x and the other is of emf y and we connect them in series we get an effective emf of x+y.  But what if we connect them in parallel, how to calculate the emf now? How to compute the  formula $\sum  \limits_{r=1}^d r \cdot 2^r$? Why does a vertical line have a SLOPE (steepness) of undefined? The curve shows :   x goes 1 to right  y goes up by 3   the SLOPE is defined as the ratio of rise to the step  SLOPE = rise/step = 3/1 = 3  Now that :   Horizontal line has SLOPE of 0. Vertical line has SLOPE of undefined.   It makes sense for me to imagine horizontal line has SLOPE of 0 since there is no rise at all. But why the vertical has SLOPE of undefined.?