http://java.sun.com/docs/books/tutorial/essential/concurrency/
> In layman's terms,
>
> 1)What really is synchronized/unsynchronized?
Synchronization is a way to ensure only one thread has access to certain resources at a time.
When you use synchronized (foo) around a block of code, the executing thread does the following:
1) Waits for the lock associated with the object pointed to by foo to become available.
2) Obtains exclusive access to that lock.
3) Executes the code in the block.
4) Releases the lock.
> 2)Under what circumstances should the synchronized be
> used ?
When you want to be sure that only one thread at a time has access to a given resource. For instance, when some action has to happen atomically.
> 3)What if I do not use synchronized?
Things that are supposed to be atomic might not be.
> Thanks
If you want to thank me, read the tutorial and do the examples.
http://java.sun.com/docs/books/tutorial/essential/concurrency/
If a method is synchronized then it can only be executed by one thread at a time.
When a thread's execution enters a synchronized method, then a lock is held on that so no other threads can use that method at the same time.
A thread that attempts to call a synchronized method that another thread has a lock on, will freeze execution of the calling thread until the first thread is done with it.
> If a method is synchronized then it can only be
> executed by one thread at a time.
Not true as worded.
Multiple threads can execute the same synced method concurrently, just not on the same object.
> When a thread's execution enters a synchronized
> method, then a lock is held on that so no other
> threads can use that method at the same time.
Again, only if they're locking on the *same* object.
> A thread that attempts to call a synchronized method
> that another thread has a lock on, will freeze
> execution of the calling thread until the first
> thread is done with it.
Again, only if its the same lock, AND the other thread could also enter the sync block if the first thread calls wait() inside that block.
Thanks for the help.
The mentioned Thread, if I understand correctly, is an object. Therefore, the Thread must have been first created. Then the synchronize can come into effect. --Is this correct?
Now if a web application (which I see) does not contain/created such a Thread object, yet I see the usage of the syncronized such as
synchronized (this){...}
1)Any idea why the synchronized() is used here(for what purpose)?
2)If no Thread object is created in a web application (I would assume that the multi-threads access scenario will not apply). In this case, if still implement the synchronized() (as the app I saw)--Is this unnecessary or useless?
Thanks again.
Scott
> Thanks for the help.
>
> The mentioned Thread, if I understand correctly, is
> an object. Therefore, the Thread must have been
> first created. Then the synchronize can come into
> effect. --Is this correct?
Everything in Java is always run inside a thread, even if you don't create one, even if you just have public static void main.... Hello World.
>
> Now if a web application (which I see) does not
> contain/created such a Thread object, yet I see the
> usage of the syncronized such as
> synchronized (this){...}
>
> 1)Any idea why the synchronized() is usedhere?
Because the servlet container has probably created multiple threads to run your web app.
> 2)If no Thread object is created in a web application
> (I would assume that the multi-threads access
> scenario will not apply). In this case, still
> implement the synchronized()--Is this unnecessary or
> useless?
If there's only one thread, there's never any need to synchronize. But when you're running a web app, unless you've configured the container for single-threaded mode, there will almost certainly be multiple threads.
For further clarification:
"Multiple threads can execute the same synced method concurrently, just not on the same object."
Does this statement means:
If it is the OBJECT that is synchronized, all the other threads MUST have to wait till the lock is released (before they can access the Object's methods); However,
if it the METHOD that is synchronized (not the Object itself), all the other threads can still access the synchronized method concurrently.
--Correct?
Scott
> For further clarification:
>
> "Multiple threads can execute the same synced method
> concurrently, just not on the same object."
>
> Does this statement means:
>
> If it is the OBJECT that is synchronized
No, the object is not syncrhonized.
We synchronize ON a lock, and that lock is associated with an object, so we might say we syncrhonize ON an object.
>, all the
> other threads MUST have to wait till the lock is
> released (before they can access the Object's
> methods); However,
Only before they access any methods that are sycned ON THAT OBJECT. Unsynchronized methods are fair game, as are synced methods that are being executed on a different object of that class.
>
> if it the METHOD that is synchronized (not the Object
> itself), all the other threads can still access the
> synchronized method concurrently.
No. The ONLY things that is ever synced are a block of code and a method, which is really just a special kind of block of code.
When you sync a non-static method, you're syncing the body on this. When you sync a static method, you're syncing the body on the Class object for that class.
class Foo {
synchronized void bar() { /* body */ }
// is the same as
void bar () {
synchronized (this) { /* body */ }
}
// AND
static synchronized void bar() { /* body */ }
// is the same as
static void bar () {
synchronized (Foo.class) { /* body */ }
}
}