File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes regarding deadlock  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "regarding deadlock  " Watch "regarding deadlock  " New topic
Author

regarding deadlock

velan vel
Ranch Hand

Joined: Nov 15, 2005
Posts: 137
1. public class SyncTest{
2. public static void main(String[] args) {
3. final StringBuffer s1= new StringBuffer();
4. final StringBuffer s2= new StringBuffer();
5. new Thread () {
6. public void run() {
7. synchronized(s1) {
8. s2.append("A");
9. synchronized(s2) {
10. s2.append("B");
11. System.out.print(s1);
12. System.out.print(s2);
13. }
14. }
15. }
16. }.start();
17. new Thread() {
18. public void run() {
19. synchronized(s2) {
20. s2.append("C");
21. synchronized(s1) {
22. s1.append("D");
23. System.out.print(s2);
24. System.out.print(s1);
25. }
26. }
27. }
28. }.start();
29. }
30. }
}
Which two statements are true? (Choose Two)
A. The program prints "ABBCAD"
B. The program prints "CDDACB"
C. The program prints "ADCBADBC"
D. The output is a non-deterministic point because of a possible deadlock condition.
E. The output is dependent on the threading model of the system the program is
running on.
Answer: D, B

------------------------------------------------------------------
$ Velan Vel @ SCJP 1.4 $
You learn From Your Failures, Others Will Learn From Your Success
------------------------------------------------------------------
Mani vannan
Ranch Hand

Joined: Aug 21, 2004
Posts: 185
hi velan,
I think that options D & E are correct.If this program runs in a round robin fashion, surely dead lock will occur(Option D). However, it all depends on the thread implementation of a system. (Option E)


Manivannan
velan vel
Ranch Hand

Joined: Nov 15, 2005
Posts: 137
} i think mani we are synchronized
Seb Mathe
Ranch Hand

Joined: Sep 28, 2005
Posts: 225
If the first thread executes before the second one, ABABCD will be printed.
If the second thread executes before the first one, CDDCAB will be printed.
But a deadlock may occur :

1. Thread1 requires lock on s1 (synchronized(s1)) -> OK
2. Thread2 requires lock on s2 (synchronized(s2)) -> OK
3. Thread1 requires lock on s2 -> WAITING
4. Thread2 requires lock on s1 -> WAITING
DEADLOCK


Regards,<br />Seb<br /> <br />SCJP 1.4
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: regarding deadlock