• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

doubt in threads

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
akaash singh
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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();
 
Wanna see my flashlight? How about this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic