About Question com.enthuware.ets.scjp.v6.2.326 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ETS User

About Question com.enthuware.ets.scjp.v6.2.326 :

Post by ETS User »

in the explaination of the question it says the following:
So, the main method creates 10 threads and each thread increments the threadcounter once. Therefore, the final value of threadcounter will be 10.

I agree. So, I selected the answer:
The final value of threadcounter just before the program terminates will be 10.

However the explaination under that selection is:
So it is possible for two threads to increment the value simultaneously and thereby causing it to increment the value only once. So the final value may theoretically less than 10.

If the later is true then the other correct answer would be subject to the same problem:
Total of 10 numbers will be printed

If we assume two threads can increment the value simultaneously, we can assume that the call to print could be called by two threads simultaneously too...so 10 numbers won't print in that case.

I choose the two answers that fit the same logical assumptions. Maybe the post incrementing has something to do with it. I don't see how though.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.326 :

Post by admin »

Hello,
Yes, the call to println may be executed by two threads simultaneously too and in that case both will print the same value. In other words, the same value will be printed twice. Therefore the total numbers printed will always be 10 even though some number might be omitted and some other number may be printed multiple times.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

zoops
Posts: 1
Joined: Wed Feb 16, 2011 8:25 am
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.326 :

Post by zoops »

Here says that this one is not correct:
The final value of threadcounter just before the program terminates will be 10.

and this is would be correct:
The final value of threadcounter just before the program terminates may be less than 10.

But, doesn't this program ends only after all threads are ended ? If so, it will have fully incremented 10 times the threadcounter.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.326 :

Post by admin »

Hello,
The issue is that the increment operation is not atomic. So it is possible that two threads may execute i++ at the same time but i is incremented by 1 (instead of 2). One increment may be lost. Therefore, the final value of threadcounter may be less than 10.

This explanation is provided with the questions. Please download the lastest version.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Guest

Re: About Question com.enthuware.ets.scjp.v6.2.326 :

Post by Guest »

The main method is creating 10 threads in the code below. Statement #1 is inside a block that synchronizes on TestClass's lock. Isn't there just one lock for this class? So, each thread will run to completion before the next one is created?

public static void main(String[] args) throws Exception
{
for(int i=0; i<10; i++)
{
synchronized(TestClass.class)
{
new TestClass().start(); // #1
}
}
}

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.326 :

Post by admin »

Guest wrote:The main method is creating 10 threads in the code below. Statement #1 is inside a block that synchronizes on TestClass's lock. Isn't there just one lock for this class? So, each thread will run to completion before the next one is created?

public static void main(String[] args) throws Exception
{
for(int i=0; i<10; i++)
{
synchronized(TestClass.class)
{
new TestClass().start(); // #1
}
}
}
Not at all. Only the creation (new TestClass()) and starting of the threads (.start()) happens in a serialized fashion because of the above synchronized block. Once a new thread is created and started, it has a life of its own. The new thread's run() method is not affected by this synchronized block because it is not synchronizing on TestClass.class object.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Guest

Re: About Question com.enthuware.ets.scjp.v6.2.326 :

Post by Guest »

admin wrote:
Guest wrote:The main method is creating 10 threads in the code below. Statement #1 is inside a block that synchronizes on TestClass's lock. Isn't there just one lock for this class? So, each thread will run to completion before the next one is created?

public static void main(String[] args) throws Exception
{
for(int i=0; i<10; i++)
{
synchronized(TestClass.class)
{
new TestClass().start(); // #1
}
}
}
Not at all. Only the creation (new TestClass()) and starting of the threads (.start()) happens in a serialized fashion because of the above synchronized block. Once a new thread is created and started, it has a life of its own. The new thread's run() method is not affected by this synchronized block because it is not synchronizing on TestClass.class object.

HTH,
Paul.
Ok, I see what you mean. The main thread acquires TestClass's lock, so it is free to create and start a new thread regardless of if the previously created threads are still alive. I was incorrectly assuming that it will only create a new thread once the previous one is dead.

javatek202
Posts: 8
Joined: Tue May 14, 2013 12:27 am
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.326 :

Post by javatek202 »

Hi,

To get result 1:
Numbers 1 to 10 printed (none repeated or missed) in serial order

we can synchronize the code in run() method as follows:

Code: Select all

public void run() {
     synchronized(TestClass.class) {
          threadcounter++;
          System.out.println(threadcounter);
     }
}
Why synchronizing on TestClass.class in each thread's run() method works, given that the main thread is still holding the lock on TestClass.class in the for loop in main() method?

Code: Select all

public static void main(String[] args) throws Exception {
    for(int i=0;i<10;i++) {
        synchronized(TestClass.class) {
             new TestClass().start();
        }	
    }
}

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.326 :

Post by admin »

The main method holds the same TestClass.class's lock only for creating and starting the threads. Not for the whole time other threads are going to be running. It just creates and starts the new threads, and then releases the lock. Other threads are then free to acquire the same lock.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

devlam
Posts: 50
Joined: Sun Nov 10, 2013 4:39 am
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.326 :

Post by devlam »

admin wrote:...
Not at all. Only the creation (new TestClass()) and starting of the threads (.start()) happens in a serialized fashion because of the above synchronized block. Once a new thread is created and started, it has a life of its own. The new thread's run() method is not affected by this synchronized block because it is not synchronizing on TestClass.class object.

HTH,
Paul.
I think in the answer the above should be mentioned. I gave an incorrect answer but didn't get the reason until I read this answer in the discussion.

Post Reply

Who is online

Users browsing this forum: No registered users and 47 guests