About Question enthuware.ocpjp.v7.2.1328 :

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

Moderator: admin

Post Reply
icepeanuts
Posts: 53
Joined: Thu Nov 22, 2012 12:01 am
Contact:

About Question enthuware.ocpjp.v7.2.1328 :

Post 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);}
}
}

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

Re: About Question enthuware.ocpjp.v7.2.1328 :

Post by admin »

What happened when you tried your code?
If you like our products and services, please help us by posting your review here.

icepeanuts
Posts: 53
Joined: Thu Nov 22, 2012 12:01 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1328 :

Post 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.

zhengye1
Posts: 17
Joined: Wed Jan 07, 2015 12:06 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1328 :

Post 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

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1328 :

Post 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?

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

Re: About Question enthuware.ocpjp.v7.2.1328 :

Post 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.
If you like our products and services, please help us by posting your review here.

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1328 :

Post 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?

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

Re: About Question enthuware.ocpjp.v7.2.1328 :

Post by admin »

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.

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1328 :

Post 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?

tugrulkarakaya
Posts: 3
Joined: Mon May 20, 2019 10:20 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1328 :

Post 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

Javier
Posts: 62
Joined: Mon Feb 20, 2017 12:31 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1328 :

Post 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

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

Re: About Question enthuware.ocpjp.v7.2.1328 :

Post 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.
If you like our products and services, please help us by posting your review here.

Javier
Posts: 62
Joined: Mon Feb 20, 2017 12:31 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1328 :

Post by Javier »

Thank you Paul! I got it now!

Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests