Forums Register Login

Threads

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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.

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
joke time: What is brown and sticky? ... ... ... A stick! Use it to beat this tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1012 times.
Similar Threads
differences between synchronized blocks and synchronized() method
I neeed answers for these
about threads, using locks of the objects in synchronized blocks
Threads
Synchronized blocks
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 17:33:21.