File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


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

Threads

alwin das
Ranch Hand

Joined: Sep 23, 2005
Posts: 32
I have following doubts regards threads topic:

1) regarding synchronized block
"it allows you to have more than one lock for code synchronization within single object"....can anyone please explain that?

does it mean:
currently executing thread,having say Object A`s lock , when come to synchronized block ,specifying object B ....
for that perticular synchronized block, thread(having Object A`s lock) is using Object B`s lock? and thus have multiple locks?


2)can we put synchronized block under synchronized method in order to have multiple objects lock?

please correct me if i am wrong....

3)Is setPriority method is always called before start() method? or can we set the priority when the thread is running?

4)is start method is final in the Thread class?or can we override it?
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Ur first two question is not clear wat u r trying to ask...

nyway jst consider these cases...



Thread acquires the lock and then calls meth method. Now since in synchronized block same object is used (this) so thread will continue with the same lock and executes synchronized block.



Whereas in this case, thread first acquires the lock of Test instance, then calls meth() method. Further acquires the lock of obj and executes synchronized block. So here same thread acquires two lock.


However if frm one synchronized method, if a call is made to another synchronized method, then thread will not acquire another lock rather it will continue with its existing lock.

Code below...



But if one method is static and another is non-static, then thread will acquire two locks... object lock and class lock.



finally frm one synchronized method, if a call is made to another synchronized method of a different class, then same thread will acquire another lock.



3)Is setPriority method is always called before start() method? or can we set the priority when the thread is running?

Once a thread starts running, u can't set its priority.

4)is start method is final in the Thread class?or can we override it?

Its not final is Thread class. You can override it. But there is no point overriding it. Wen u override start() method, then automatically run() method will not be called in that case.

start() method of Thread class calls internally start0() method which is native not written in java and also private.

Since its private u can't make a call to start0() in overriding start() method. So ur start() method will not behave in the same way as of Thread class start().

If u override start() method, then seperate thread will not be created.

regards
[ June 10, 2006: Message edited by: Naseem Khan ]

Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Alexandre de Paula
Greenhorn

Joined: May 15, 2006
Posts: 8
3)Is setPriority method is always called before start() method? or can we set the priority when the thread is running?

This piece of code shows that you can change thread priority after calling start() method.

Swapnil Trivedi
Ranch Hand

Joined: Jun 06, 2006
Posts: 106
Hi:
Once u call start() method, ur thread is in Runnable state, it's ready to run but not "running"( It will go into the queue of threads that are ready to run, and it depends upon thread scheduler to select any thread for execution). So it will wait there for execution.
On the other hand... the rest of ur code is executed normally and with guarenteed behaviour. So, ur proirity will be set before ur thread starts running.

This is my understanding. I request all to correct me if I am wrong.


Regards
swapnil


SCJP 5.0<br />-----------<br />"Help Ever && Hurt Never"
vathsala nagaraju
Greenhorn

Joined: May 29, 2006
Posts: 4
Hi alwin das ,
I can explain u with an example

class Obj
{
synchornized void get(){ set()........... }
synchornized void set(){........... }
}

u have created two threads A and B.
When A calls obj.get()it locks the object. when it executes set() inside get(),thread A can contiue execution becoz thread A is already holding lock for the same object.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads