• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Mock Question Doubt...EQ+

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question ID : com.enthuware.ets.scjp.v5.2.326

Hello Friends,

Consider the following program:
(See Exhibit.)
Which of the following is correct regarding the execution of the given program?



Please select 2 options :

A.Numbers 1 to 10 will be printed (none repeated or missed) in a serial order.
B.Numbers 1 to 10 will be printed (none repeated or missed) in any order.
C.The final value of threadcounter just before the program terminates will be 10.
D.Total of 10 numbers will be printed
E.The final value of threadcounter just before the program terminates may
be less than 10.

The correct ans given is D and E..

Isn't B also a right answer..?

Can anyone please give their suggestions on the above problem.Thank you in advance.

Regards,
Hardik.S.Raja
 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


B.Numbers 1 to 10 will be printed (none repeated or missed) in any order

Isn't B also a right answer..?



Hi Hardik,
Since the variable is static and the lock is on the class. Everytime when the run method is called variable increments and because its static all the objects of the class share the same variable so output will be 1 to 10
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This or a similar question was discussed about a day or two ago.

Points that would help you to understand the question :

1. When you synchronize a block and have the Thread.start() method in it, it doesn't mean that the run method would also run in the synchronized block.

2. threadcounter++ is not an atomic operation. It is like threadcounter = threadcounter + 1; so it is possible that two threads recieve the same value of threadcounter and increment it at the same time so that would result in a threadcounter++ kind of going waste. Had it been an atomic operation then also due to the java memory model still it is not gauranteed that operation is thread safe. Use of volatile is suggested here.

Having said all this I don't think this level of question would not be asked in the SCJP exam.
 
Hardik Raja
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To add one more question to my doubt...

I think when the number of threads are more than 10 say...1000 ..
numbers printed might be in range of 0-1000...but there would be a possibility of a number getting repeated.

Consider the below situation

Say thread 1 executes threadcounter++; and sets threadcounter to 998 and gets out of running, now another thread 2 executes threadcounter++; and sets threadcounter to 999 and also prints it 999..after this Thread 1 returns to running and prints 999(as threadcounter is static)
so 999 getting printed 2 times....

NOTE: THIS IS NOT GUARANTEED AS ITS RELATED TO THREADS

Considering the above what do you feel is this possible?
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to me the answer would be C & D.
The only thing guaranteed about this piece of code is that:

1. threadcounter will be incremented 10 times.( so being a static variable its value will be 10 at the end of all the loops)
2. threadcounter will be printed 10 times.

based on these two , I do not see how E would be an answer.
Also, there would be no guarantees about which numbers will be printed or in what order will they be printed because the run method by itself is not syncronized.
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check this out Ealier post
 
It sure was nice of your sister to lend us her car. Let's show our appreciation by sharing this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic