About Question enthuware.ocpjp.v7.2.1328 :
Moderator: admin
-
- Posts: 53
- Joined: Thu Nov 22, 2012 12:01 am
- Contact:
About Question enthuware.ocpjp.v7.2.1328 :
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);}
}
}
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);}
}
}
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1328 :
What happened when you tried your code?
If you like our products and services, please help us by posting your review here.
-
- Posts: 53
- Joined: Thu Nov 22, 2012 12:01 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1328 :
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.
-
- Posts: 17
- Joined: Wed Jan 07, 2015 12:06 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1328 :
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
The output will be first 100 sets of value for x and y is for Thread 1, last 100 sets for Thread 2
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();
}
}
-
- Posts: 85
- Joined: Mon Dec 24, 2018 6:24 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1328 :
In a nutshell, synchronized method only allow one thread access at a time. And these threads were created by same object. Is it right?
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1328 :
>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.
Correct.
>And these threads were created by same object.
I am not sure what you mean by this.
If you like our products and services, please help us by posting your review here.
-
- Posts: 85
- Joined: Mon Dec 24, 2018 6:24 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1328 :
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?
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1328 :
Sorry, still doesn't make any sense. Objects don't acquire locks, threads do.
If you like our products and services, please help us by posting your review here.
-
- Posts: 85
- Joined: Mon Dec 24, 2018 6:24 pm
- Contact:
-
- Posts: 3
- Joined: Mon May 20, 2019 10:20 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1328 :
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
-
- Posts: 66
- Joined: Mon Feb 20, 2017 12:31 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1328 :
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
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
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1328 :
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 66
- Joined: Mon Feb 20, 2017 12:31 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1328 :
Thank you Paul! I got it now!
Who is online
Users browsing this forum: No registered users and 100 guests