• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread Synchronization

 
Tom Mark
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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
Posts: 148
Hibernate Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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?

Thanks
 
Tom Mark
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?





Thanks.
 
Andrey Kozhanov
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 148
Hibernate Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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



If not,Ranchers please correct me if am wrong.

Thanks
 
Tom Mark
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I have understood.

Thank you both.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic