Link to home
Start Free TrialLog in
Avatar of mte01
mte01Flag for Lebanon

asked on

Java's keyword synchronized

Hey experts,

I read the Java documentation about threads and the interface Runnable, but I kind of couldn't understand the word synchronized (when used in a function header): Does that mean that an entering thread locks mutex before entering this function so it can have sole access to it or does it mean that instructions inside the function are "synchronized" between threads (also sometimes it seemed to mean that threads can be created inside synchronized functions or something like that)

Any explanations would be really helpful
ASKER CERTIFIED SOLUTION
Avatar of sciuriware
sciuriware

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mte01

ASKER

>> A thread by itself has only one "life" so you can't synchonise it.

What does that mean??
Avatar of sciuriware
sciuriware

A thread is a single phenomenon: it runs or not.
You can't tell it to wait because it is doing something itself.
From 2 threads on you can.
;JOOP!
Avatar of mte01

ASKER

Ahaa....what you mean is that with a single thread you don't have to do synchronization (yeh sure that's obvious)
Avatar of mte01

ASKER

I'll keep the question now open for like an hour for any new useful comments...
Avatar of mte01

ASKER

I guess that's it......what sciuriware said was more than enough....
One more: be aware that even a tiny program (hello world) has several threads running.
But these are started from the JVM and may be considered safe and balanced.
For you the main thread may be the only thread to account for.
Things get more complex by adding a GUI, which introduces several new threads.
Almost every week you will find some question in this forum that rises
from putting a GUI thread to work from itself.
I wrote some applications with 100's of concurrent threads; only then
things get difficult; unless you know exactly where and how those interfere.
NOT WHEN! you just can not predict WHEN events will interfere.
Therefore we try to control events by 'synchronised' methods and objects.
;JOOP!