aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt in threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt in threads" Watch "doubt in threads" New topic
Author

doubt in threads

akaash singh
Ranch Hand

Joined: Jul 04, 2009
Posts: 45
Synchronization problems occur when two threads have a access to the same object . In the following code :



In this we have two threads t1 and t2 . The common object they share is "r" . " r" has access to x ; thereby granting t1 and t2 access to x , leading to synchronization problems in the output .

i have the following doubts :
- is the above reasoning correct ?
- the objetc on which run() is executed is "r" and not t1 and t2?

And when we extend the Thread class the run() is executed on the object of the subclass of Thread ? as in :



So here the run() is executed on ob?

Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

Runnable interface is implemented by the thread class also. Well now if you class is implementing a runnable interface and then providing a run method means you are providing a job that has to be performed by the thread. So we pass the job(runnable reference) to the thread. Extending thread class is not appropriate because extending classes should always be done when you have a special purpose. That's why its also called specialization. Also thread class has a start method which internally makes a new stack for the thread and calls the run method.

Well when you pass a runnable object, you are telling that instead of using the thread's default run, the thread should use your run method i.e the job.


In this you can solve the synchronization problem by using the synchronized block on this. Why this because you only creating one object of class T1 which creates 2 threads. Also you have the same job to be performed but the threads will run their own runs. Means the same job is to be performed by 2 threads i.e 2 times simultaneously in different stack modifying the x. So stop that synchronize on the T1 class object which is only 1. So before entering the synchronized block the thread will acquire the lock and thus only 1 thread can manipulate the x variable.


I hope i answered what you asked for. If not please correct me ok

Cheers.


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
akaash singh
Ranch Hand

Joined: Jul 04, 2009
Posts: 45
i understand that we can synchronize by making the following change :

public synchronized void run() {//}

but what does "this" refer to : is it the object "r" of class Run ?
or is it the object of class T1.
The 2 threads must have access to the same object in order to cause synchronization problems. What is that same object here: "r" or the "anonymous object of class T1"
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
akaash singh wrote:What is that same object here: "r" or the "anonymous object of class T1"


"r" of T1's anonymous object.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
akaash singh wrote:What is that same object here: "r" or the "anonymous object of class T1"


"r" of T1's anonymous object.
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

but what does "this" refer to : is it the object "r" of class Run ?
or is it the object of class T1.

this is put in which class?

Well don't confuse yourself by this. Runnable gives the contract that the particular class can have its own methods to run that's all. Runnable interface only has 1 method so its reference can always run that method only. Also within the class implementing the Runnable it will always be its object(this) because the JVM looks at the object that is new Run();
 
Don't get me started about those stupid light bulbs.
 
subject: doubt in threads