Page 1 of 1

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

Posted: Tue Dec 28, 2010 10:24 pm
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.

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

Posted: Fri Dec 31, 2010 9:45 am
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.

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

Posted: Wed Feb 16, 2011 8:42 am
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.

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

Posted: Wed Feb 16, 2011 10:37 am
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.

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

Posted: Sat Dec 03, 2011 12:45 pm
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
}
}
}

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

Posted: Sat Dec 03, 2011 1:36 pm
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.

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

Posted: Sat Dec 03, 2011 2:09 pm
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.

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

Posted: Wed Oct 02, 2013 5:56 pm
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();
        }	
    }
}

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

Posted: Wed Oct 02, 2013 6:37 pm
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.

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

Posted: Sun Dec 22, 2013 8:24 am
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.