This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes synchronized Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "synchronized" Watch "synchronized" New topic
Author

synchronized

Venkat Ramsimha
Ranch Hand

Joined: Dec 28, 2004
Posts: 127
1.If not explicitly set, a thread�s priority will be the same priority
as the thread that created this thread.
is above true to be said?if that is the case whats the priority for the
thread?

2.Assume you have a class that holds two private variables: a and b. Which of the following
pairs can prevent concurrent access problems in that class?

answer1: public synchronized int read(int a, int b)
{
return a+b;
}
public synchronized void set(int a, int b)
{
this.a=a;this.b=b;
}

answer2: public int read(int a, int b)
{
synchronized(this)
{
return a+b;
}
}
public void set(int a, int b)
{
synchronized(this)
{
this.a=a;this.b=b;
}
}

can anybody give the explanation for the above answers


thanks
venkat ram simha
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
As far as I can see, both cases require the current thread to acquire a lock on the object being modified, so in both cases it is not possible to modify variables a and b concurrently.

Let us assumet there are two threads, A and B. If thread A acquires the lock on the this object first, then thread b has to wait till the other thread finishes the modification to acquire the lock on the object, so thread B be will be put on the waiting list to acquire the lock, once thread A has released the lock on the object, thread B might acquire the lock.

So there is no way this methods are executed concurrently by two or more threads; provided that the class does not offer other methods but this that you showed us in your code.
[ April 30, 2005: Message edited by: Edwin Dalorzo ]
Alex Belisle Turcot
Ranch Hand

Joined: Apr 26, 2005
Posts: 516
for your first question, isnt it rather that all thread created are by default set to priority 5( Thread.NORM_PRIORITY )

Alex
[ April 30, 2005: Message edited by: Alex Turcot ]
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
[venkatramsimha]: 1.If not explicitly set, a thread�s priority will be the same priority as the thread that created this thread.
is above true to be said?


Yes. That's right.

Quoted from Thread API:
------------------------
Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority. Each thread may or may not also be marked as a daemon. When code running in some thread creates a new Thread object, the new thread has its priority initially set equal to the priority of the creating thread, and is a daemon thread if and only if the creating thread is a daemon.
------------------------

Here is an example which demostrates that Thread b spawned by Thread a has the same priority as Thread a.


Joyce
[ April 30, 2005: Message edited by: Joyce Lee ]
Alex Belisle Turcot
Ranch Hand

Joined: Apr 26, 2005
Posts: 516
ok, it makes sense.. thx Joyce
Venkat Ramsimha
Ranch Hand

Joined: Dec 28, 2004
Posts: 127
Thanks for all of u.but still iam not clear with the synchronized concept.can anybody provide some programs for making me to be clear

thanks
venkat
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
hi all

The Daemon thread is one that runs at the background till all the thread that relates to the application finishes including main thread....

i didn't understand how a daemon thread is spawning another thread....

There should be only one daemon thread for one application...

any comments
Alex Belisle Turcot
Ranch Hand

Joined: Apr 26, 2005
Posts: 516
Hi,

to my understanding, a deamon thread is exactly like a normal thread except for its status. The program will run as long as a normal thread is running. So, if only deamon threads are running, the program will stop.

It makes sense to have more than one deamon. Several deamon threads can do different housekeeping stuff that should not be running when all normal threads are stopped.

Alex
Alex Belisle Turcot
Ranch Hand

Joined: Apr 26, 2005
Posts: 516
For "synchronized", here is my understanding of it, please anyone correct me if im wrong (i'm also learning all this stuff...)

When you use synchronized you have to specify which object you wish to lock.
Locking an object means that no one will be able to lock it on top of you.

So, when you synchronize on an object, the object must not be locked by another synchronized block.

Locking an object does not prevent normal modification to the object, it only prevent someone else to do another synchronize on it. So, your code has to be consistent for this concept for be worth it.

If you try to synchronize an object already locked, the thread will wait until the object is unlock.

Because of this, imagine you have a lock on A, and you want to lock B also. but B is locked and is requesting a lock on A.

In this case, you have a deadlock: both thread are waiting for the other thread's lock to be unlock.

bye,
Alex
[ May 02, 2005: Message edited by: Alex Turcot ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: synchronized
 
Similar Threads
Threas - Object Locks Question
Thread question
Threads Questions?
Doubt in K&B Question on Thread
Why autoboxing doesnt work here???