Solved

Java's keyword synchronized

Posted on 2004-09-09
7
343 Views
Last Modified: 2012-06-21
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
0
Comment
Question by:mte01
  • 4
  • 3
7 Comments
 
LVL 24

Accepted Solution

by:
sciuriware earned 200 total points
ID: 12018540
synchonised is a tag to methods and objects.
It means that provisions are (to be) made so that not 2 or more threads will try to modify
the data and/or to call the method(s) concurrently.
A thread by itself has only one "life" so you can't synchonise it.
A thread calling a synchronised method will "make sure" no other thread "is there"
and will wait until the other one is finished.

There's a lot more, but I recommend you look at the tutorial(s) on the SUN site.

;JOOP!
0
 
LVL 3

Author Comment

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

What does that mean??
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12018738
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!
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Author Comment

by:mte01
ID: 12018848
Ahaa....what you mean is that with a single thread you don't have to do synchronization (yeh sure that's obvious)
0
 
LVL 3

Author Comment

by:mte01
ID: 12019137
I'll keep the question now open for like an hour for any new useful comments...
0
 
LVL 3

Author Comment

by:mte01
ID: 12020067
I guess that's it......what sciuriware said was more than enough....
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12023895
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!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Understanding websocket example in spring 1 57
Java Server Faces parameter pass? 6 39
Basic Java Case or If-Else statement... 3 43
servlet filter example 37 43
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now