aspose file tools*
The moose likes Threads and Synchronization and the fly likes Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Threads" Watch "Threads" New topic
Author

Threads

Dal Singar Yadav
Greenhorn

Joined: Jan 09, 2012
Posts: 4

Is the statement--

"Just before the main method exits, the account’s number field is guaranteed to have value 2000"
is true.

If Yes -- why ?
If No -- why No ?

Please explain in detail.


Thanks&Regards
Vijay Kumar
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1506
    
    5

Hello Dal Singar Yadav,

Welcome to CodeRanch.

Please, UseCodeTags, QuoteYourSources & ShowSomeEffort.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
sridhar chandramouli
Greenhorn

Joined: Apr 13, 2011
Posts: 6
To get 2000, synchronize on account before you setNumber(). Otherwise you've got a race condition. While both setNumber and getNumber are individually synchronized, the entire operation that you have inside the for loop is not synchronized.



And what the previous poster said.
Dal Singar Yadav
Greenhorn

Joined: Jan 09, 2012
Posts: 4
Dal Singar Yadav wrote:
Is the statement--

"Just before the main method exits, the account’s number field is guaranteed to have value 2000"
is true.

If Yes -- why ?
If No -- why No ?

Please explain in detail.


Thanks&Regards
Vijay Kumar



Thanks Sridhar for answer. But I want to ask how two threads can call getNumer() or SetNumer() method at the same time from run() method because each one is synchronized.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18842
    
  40


Please QuoteYourSources


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Dal Singar Yadav
Greenhorn

Joined: Jan 09, 2012
Posts: 4
Dal Singar Yadav wrote:




Thanks&Regards
Vijay Kumar



Q. Why this program prints 999999. According to me main has synchronized block on thread object. First of all how it is notified without any notify method and it may print any number from 1 to 999999 but result is always 999999.
Please clearly explain.

Dal Singar Yadav
Greenhorn

Joined: Jan 09, 2012
Posts: 4
Henry Wong wrote:
Please QuoteYourSources


The following question taken from downlodable.com's scjp mock exam.



public class Threads1
{
int x=0;

public class Runner implements Runnable
{

public void run()
{
int current=0;
for(int i=0;i<4;i++)
{
current=x;
System.out.print(current + " ,");
x= current +2;
}
}
}

public static void main(String[] args)
{
new Threads1().go();
}


public void go()
{

Runnable r1=new Runner();
new Thread(r1).start();
new Thread(r1).start();
}

}



The possible outputs are as flollows:

0, 2, 4, 4, 6, 8, 10, 6,
0, 2, 4, 6, 8, 10, 12, 14,

Q. why these outputs are coming. Please explain


And Suggest Any book for Thread topic as I am preparing for SCJP 6.0 except Kathy sierra and Khalid Mughal.
When I read these books I think I have covered this topic but in some questions I can't predict possible answers.
Please suggest.

Thanks & Regards
Vijay Kumar
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1506
    
    5

Hello Dal Singar Yadav,

Welcome to CodeRanch!

As some of the posts already mention, please, QuoteYourSources ('scjp mock exam' is not enough - please tell us the details), UseCodeTags (otherwise, it becomes unnecessarily difficult to read the code) and ShowSomeEffort (where exactly you are stuck? what have you tried so far?)

Also, please limit one question per topic. You can ask further questions related to that particular code snippets in same topic, but for new code snippet, please create a new thread(topic).

Dal Singar Yadav wrote:Q. What are the possible outputs of this program and why ? Please explain clearly.

What possible outputs you've thought so far? Please note that this is a learning forum, and people here would be more than happy to help you learn. It is perfectly normal to get stuck during learning phase However, asking and providing ready-made answers is discouraged.

Dal Singar Yadav wrote:And Suggest Any book for Thread topic as I am preparing for SCJP 6.0 except Kathy sierra and Khalid Mughal.

Again, what exact part from Sierra-Bates and/or Mughal book is causing you trouble? I guess those two books are more than enough for every topic of OCPJP 6.

I hope this helps.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads