Synchronization on object, java I am reading the Oracle tutorials about multithreading programming in Java. I don't understand why should I create a new object to sync the some part of code? For what reason does the creation of new dummy object serve?  I do understand that creating these two objects will prevent compiler from reordering the code segment guarded by the construction syncronized(lock1){}  However, I would like to know can I use any other objects (except MsLunch) in the construction syncronized(lock1){} ?  What is the motivation behind introducing such construction syncronized(lock1){} ?  Here is the piece of code, I am concerned with:  public class MsLunch {        private long c1 = 0;        private long c2 = 0;        // what is the purpose of these two objects? how do they serve as locks?        private Object lock1 = new Object();        private Object lock2 = new Object();         public void inc1() {               synchronized(lock1) {                    c1++;               }        }         public void inc2() {               synchronized(lock2) {                    c2++;               }         }  }
Avoid synchronized(this) in Java? 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
Sci-fi series: humans are immune to telepathy, become soldiers for an alien group fighting against invertebrate telepaths Contribute to bounty? Example of a recursive set $S$ and a total recursive function $f$ such that $f(S)$ is not recursive? Browsing wikipedia, I stumbled on the following: "The image of a computable set under a total computable bijection is computable." Given the form of the theorem, there must be some example of a example of a recursive set $S$ and a total recursive function $f$ such that $f(S)$ is not recursive (otherwise the statement would have presumably been stronger). But I'm having difficulty dreaming up a case where this happens. Book recommendations for fluid dynamics self-study I have my bachelors degree in engineering science: computer science, option electrotechnic. I have a good understanding of basic mechanics and dynamics, calculus (differential equations).  The books I'm now having a look at are:       What book do you recommend for studying fluid dynamics on my own and why? CUDA writing to constant memory wrong value How to use a special character as a normal one in Unix shells? In The Matrix Reloaded, the Architect reveals to Neo that Trinity is about to die. I can't help but wonder if Neo would still choose the left door if the Architect had withheld that information.  The Architect could have designed a better choice mechanism so as to trick Neo into making a choice in the machine's favor rather than a highly risky one.   Did I miss anything important in the movie? OpenLayers low resolution and/or shifts in QGIS print composer? Photoelectric effect intensity I understand the PE effect quite well but I'm failing to understand one thing. Intensity is the amount of energy per second incident to a given area.   So can you can increase the intensity by either increasing the electrical energy or making the area smaller? By increasing the electrical energy, what exactly is happening?  I assume that by increasing intensity you are increasing the number of photons incident to an area of the metal thus increasing the rate of photoelectric emission. But why doesn't increasing intensity via more electrical power lead to greater energy of the electrons, if by increasing intensity you are increasing the energy of the incident waves?  I understand the results but I'm trying to understand why the intensity works the way it is thanks How do I create an .exe for a Java program? How do I remove duplicates from a C# array? I have been working with a string[] array in C# that gets returned from a function call. I could possibly cast to a Generic collection, but I was wondering if there was a better way to do it, possibly by using a temp array.  What is the best way to remove duplicates from a C# array? Einstein's big revelation was that time and space are inseparable components of the same fabric. Physical observation tells us that distant galaxies are moving away from us at an accelerated rate, and because of the difficulty (impossibility?) of defining a coordinate system where things have well defined coordinates while also moving away from each other without changing the metric on the space, we interpret this to mean that space itself is expanding.   Because space and time are so directly intertwined is it possible that time too is expanding? Or perhaps it could be contracting? Commenting out large sections Add new alias to Gmail without SMTP (forwarding-only address) I used to be able to go to Accounts and Import → Add another email address you own and add an email account that's only a forwarding address. That is, it's not a mailbox and I don't have SMTP access to it.  Gmail would send me a confirmation email to that address, I'd confirm it's mine, and done. Now it only gives me the option to add SMTP credentials.  Did they stop supporting the previous method? Is there any other way I can get that option back? Usernames as Advertising or Spam? Proteins are synthesized on ribosomes from mRNA copies of regions of the DNA. But ribosomes themselves are made up of proteins (and RNA). So how could the first ribosomes have arisen? Was there previously some other way of making proteins other than by ribosomes and mRNA? What happened to this user's rep, badges, and name? This user is only displaying 56 rep.   But surely (s)he has much more than that.        The user has also never given out any bounties.  Also, badges are missing: In JavaScript can I make a "click" event fire programmatically for a file input element? I'd like to make a click event fire on an &lt;input type="file"&gt; tag programmatically.  Just calling click() doesn't seem to do anything or at least it doesn't pop up a file selection dialog.  I've been experimenting with capturing events using listeners and redirecting the event, but I haven't been able to get that to actually perform the event like someone clicked on it. When the ball is being bounced by the goalkeeper from his hands and a player steals it without touching the goalkeeper (while the goalkeeper is not touching the ball) and scores, is it legal or not?   shows what I'm talking about. Could any one give an example of a bijective map from $\mathbb{R}^3\rightarrow \mathbb{R}$?  Thank you.