Page 1 of 1
About Question enthuware.ocpjp.v7.2.1328 :
Posted: Sun Mar 09, 2014 2:46 am
by icepeanuts
One more question.
If the run() is defined as the following, it will keep on printing same values for x and y incrementing by 1 on each line. Is it correct?
public void run() {
synchronized(Test.class) {
for(;;)
{x++;y++;System.out.println(x+" "+y);}
}
}
Re: About Question enthuware.ocpjp.v7.2.1328 :
Posted: Sun Mar 09, 2014 3:27 am
by admin
What happened when you tried your code?
Re: About Question enthuware.ocpjp.v7.2.1328 :
Posted: Sun Mar 09, 2014 4:26 am
by icepeanuts
I checked the output and found it keeps on printing same values for x and y incrementing by 1 on each line if I am not wrong.
Re: About Question enthuware.ocpjp.v7.2.1328 :
Posted: Fri Sep 28, 2018 10:31 am
by zhengye1
If my assumption is correct, the second thread will never get the chance to run until the first one finish? Because I modified the run like this
Code: Select all
public class ThreadExample extends Thread {
private String name;
static int x, y;
public ThreadExample(String name) {
this.name = name;
}
public void run() {
synchronized (ThreadExample.class) {
for (int i = 0; i < 100; i++) {
x++;
y++;
System.out.println(this.name + ":" + x + " " + y);
}
}
}
public static void main(String[] args) {
new ThreadExample("Thread 1").start();
new ThreadExample("Thread 2").start();
}
}
The output will be first 100 sets of value for x and y is for Thread 1, last 100 sets for Thread 2
Re: About Question enthuware.ocpjp.v7.2.1328 :
Posted: Sun Mar 03, 2019 10:30 pm
by crazymind
In a nutshell, synchronized method only allow one thread access at a time. And these threads were created by same object. Is it right?
Re: About Question enthuware.ocpjp.v7.2.1328 :
Posted: Sun Mar 03, 2019 11:40 pm
by admin
>synchronized method only allow one thread access at a time.
Correct.
>And these threads were created by same object.
I am not sure what you mean by this.
Re: About Question enthuware.ocpjp.v7.2.1328 :
Posted: Tue Mar 05, 2019 5:04 pm
by crazymind
admin wrote: ↑Sun Mar 03, 2019 11:40 pm
>synchronized method only allow one thread access at a time.
Correct.
>And these threads were created by same object.
I am not sure what you mean by this.
By that I mean, it will check if same Test object acquire the lock in order to prevent same object invoke this method second time. Right?
Re: About Question enthuware.ocpjp.v7.2.1328 :
Posted: Tue Mar 05, 2019 10:03 pm
by admin
Sorry, still doesn't make any sense. Objects don't acquire locks, threads do.
Re: About Question enthuware.ocpjp.v7.2.1328 :
Posted: Tue Mar 05, 2019 10:53 pm
by crazymind
admin wrote: ↑Tue Mar 05, 2019 10:03 pm
Sorry, still doesn't make any sense. Objects don't acquire locks, threads do.
Can I say thread can only acquire the lock on current instance but there are two threads acquire lock on two different instances?
Re: About Question enthuware.ocpjp.v7.2.1328 :
Posted: Fri May 24, 2019 5:48 am
by tugrulkarakaya
I have just learned that synchronized is not class level lock it is instance lock. interesting. and this also answers crazymind's question I think
Re: About Question enthuware.ocpjp.v7.2.1328 :
Posted: Sat Sep 05, 2020 2:45 pm
by Javier
Hi Paul!
I am confused, when I run this code the results are the same as the third answer (x and y different values but increment by 1 on each line).
The right answer is "you can not say anything about the values".
Is this the right answer because there are 2 different threads operating at the same time? (the theory says: "the order of thread execution once the threads have been started is indeterminate")
Thank you
Re: About Question enthuware.ocpjp.v7.2.1328 :
Posted: Sun Sep 06, 2020 9:26 am
by admin
Yes, not only that there are two different threads, they are not synchronizing on the same lock. That is why the shared variable x or y might be incremented by one thread, then by second thread, and then the value is printed. In this scenario, the output will show that the value incremented by 2 and then by 0.
Re: About Question enthuware.ocpjp.v7.2.1328 :
Posted: Sun Sep 06, 2020 12:55 pm
by Javier
Thank you Paul! I got it now!