Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Question - Lock on Object  RSS feed

 
Meghna Bhardwaj
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using the Head First, K&B book there is the example of Ryan and Monica's bank account problem, where it gets Overdrawn due to withdrawal method not being synchronized. I have gone thru this many times and also experimented with it in Eclipse. Below is code snippet:

public class RyanMonicaJob implements Runnable{
public static void main(String[] args) {
RyanMonicaJob job = new RyanMonicaJob();
RyanMonicaJob job = new RyanMonicaJob();
Thread one = new Thread(job);
Thread two = new Thread(job);
one.start();
two.start();
}
private void makeWithdrawal(int amount){
///Makes the withdrawal
}
}

In the main(), if I were to replace above lines with
RyanMonicaJob r1 = new RyanMonicaJob();
RyanMonicaJob r2 = new RyanMonicaJob();
Thread one = new Thread(r1);
Thread two = new Thread(r2);

so that we are not using the same job object for the threads, then no Overdrawal occurs. Instead of the solution making the method synchronized, why not use a completely new object as above for second thread.

Sorry, if this is a really non-sensical question, but I am really unclear about this issue.

Thanks.
 
Chandramouli Ram
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Meghna,

I don't have the K&B book you refer to, still I can possibly give my comments. From the code snippet provided:

1. RyanMonicaJob job = new RyanMonicaJob();
2. RyanMonicaJob job = new RyanMonicaJob();

Based on the above code, the first job object will subsequently be garbage collected as there are no more references to it after line 2 is executed and I just consider it a typo.

If you consider the code you suggested:

RyanMonicaJob r1 = new RyanMonicaJob();
RyanMonicaJob r2 = new RyanMonicaJob();
Thread one = new Thread(r1);
Thread two = new Thread(r2);

you are basically creating 2 different objects and you are making sure thread one acts on r1 only and and thread two on r2 only. So obviously there are no thread related issues (racing conditions) here. The interesting thing happens when 2 or more therads attempt to alter the state of a single object, which is what the sample code is providing.
If you have worked with Servlets, there is generally one instance of a servlet and all client requests (each being a separate thread) invokes methods on this single servlet instance. Not sure if this example helps!
 
Meghna Bhardwaj
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI Ram,

Yes you are right it is a typo, only one instance of job is created and passed to 2 threads. I guess there are a lot of practical situations where different threads must act on the same object.

I am new to Java and do not have experience in servlets. Thanks for your reply, if there are any other examples you can give it would be nice.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess there are a lot of practical situations where different threads must act on the same object.


Yup. We could maybe pass a logger to two threads so they could both write to the same log. Or some kind of "collector" object that would gather up the output from both threads. In the servlet world, the container makes just one instance of each servlet and passes it to every thread that needs it.

Any time this happens, we have to worry about whether that shared resource is thread safe. Start another thread if you want to talk about how to do that.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Meghna Bhardwaj:
so that we are not using the same job object for the threads, then no Overdrawal occurs.


This way there would be a two different bank accounts, if I am not mistaken, and the primary goal would get invalidated.
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Adeel Ansari:


This way there would be a two different bank accounts, if I am not mistaken, and the primary goal would get invalidated.


I think she meant to say for the below lines of code



As Ram explained its a typo and the object being pointed out by "job" would be garbage collected after the execution of step 2.

Irrespective of it, the compiler would have thrown an error saying that "job is already defined in main"
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!