File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Thread Synchronization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Thread Synchronization " Watch "Thread Synchronization " New topic

Thread Synchronization

Tom Mark

Joined: Jun 15, 2011
Posts: 27
I'm continuing to do my self-test on SCJP and I can't understand Thread Synchronization things.
Can anyone tell help me why the answer is C and E ?

3. class Chicks {
4. synchronized void yack(long id) {
5. for(int x = 1; x < 3; x++) {
6. System.out.print(id + " ");
7. Thread.yield();
8. }
9. }
10. }
11. public class ChicksYack implements Runnable {
12. Chicks c;
13. public static void main(String[] args) {
14. new ChicksYack().go();
15. }
16. void go() {
17. c = new Chicks();
18. new Thread(new ChicksYack()).start();
19. new Thread(new ChicksYack()).start();
20. }
21. public void run() {
22. c.yack(Thread.currentThread().getId());
23. }
24. }

Which are true? (Choose all that apply.)
A. Compilation fails
B. The output could be 4 4 2 3
C. The output could be 4 4 2 2
D. The output could be 4 4 4 2
E. The output could be 2 2 4 4
F. An exception is thrown at runtime

new Thread(new ChicksYack()).start(); and another new Thread(new ChicksYack()).start(); are two different objects,
so I thought that the answer could also be 4 2 4 2 or 2 4 2 4, since different instances do not interfere with each other,
but when I checked the code in compiler I saw that I was wrong.
Vijay Tidake
Ranch Hand

Joined: Nov 04, 2008
Posts: 146

I am not very much clear about it but what I think is this will throw the runtime exception at c.yack(Thread.currentThread().getId());

because the variable "c" is not initialised.

Hope I am correct?


The important thing is not to stop questioning.Curiosity has its own reason for existing.
Tom Mark

Joined: Jun 15, 2011
Posts: 27
You are right. "c" has not been assigned to an object. Sorry, I've missed it. I had to make "c" static on line 12.
But imagine that everything is Ok with this and c is initialised.

The problem is that I don't understand synchronization on two different objects?

Andrey Kozhanov
Ranch Hand

Joined: Mar 12, 2010
Posts: 79
Vijay is right, the code you provided will indeed fail in runtime with NullPointerException. But if you change variable c to be static (on line 12), it will be initialized on line 17 and answers will be indeed C and E.

Two new threads are started on lines 18 and 19. We don't know which one will actually start first, so right answer could start with any thread ID (in our case 2 and 4). But we have only two threads, so only two thread IDs, that's why answer B is wrong. Neither thread is daemon thread and neither thread is interrupted in any way, so yack method will be executed in full, i.e. will output thread ID two times, that's why answer D is wrong. Thread.yield() on line 7 will pause current thread to allow other threads to be executed, but yack method is synchronized. That means it could be rewritten as follows: I.e. only one thread could call yack method of the same instance of Chicks class in the same time. With changes to line 12 i proposed above we will have single instance of Chicks class, i.e. both threads will be executed one by one. Considering that we don't know which out of two our threads will actually start first, right answers are C and E.
Vijay Tidake
Ranch Hand

Joined: Nov 04, 2008
Posts: 146


Please see the following code.Hope I am able to explain it correctly.

If not,Ranchers please correct me if am wrong.

Tom Mark

Joined: Jun 15, 2011
Posts: 27
I think I have understood.

Thank you both.
I agree. Here's the link:
subject: Thread Synchronization