Java's keyword synchronized

Posted on 2004-09-09
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
Question by:mte01
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
LVL 24

Accepted Solution

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.


Author Comment

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

What does that mean??
LVL 24

Expert Comment

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.
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.


Author Comment

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

Author Comment

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

Author Comment

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

Expert Comment

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.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
String array comparison 4 58
SequenceInputStream example 3 38
ejb entity bean example issue 2 43
Need help aligning JRadioButton that put in a box using GridBagLayout 2 42
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

751 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