• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regarding Thread

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Below is the question. The ans is DE but mine is AB
61. Click the exhibit button:
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.s1.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.
I think the program create 2 objects of 2 annymous inner classes extends Thread in class SynTest and call start() immediately. In every annymous class run() method, there is a synchronized block till to the end of the run(), and this block also embeded another synchronized block but it cannot affect the outer block I think. Thus run() will execute all the codes in it without any interrupting! Of course we cannot ensure which thread will run first. so the answer I think is AB.
But the standard answer say it should be DE, Why?
Thank you !
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I might be wrong but ...
E is correct as threading is system dependent and so any output is also system dependent.
D is correct as there is a possibility of deadlock in the code (Again this would depend upon thread implementation). Consider the following scenario. The first thread locks s1 and performs an append. It then tries to obtain a lock to s2. If at this point the other thread has already locked s2 it can't have the lock so will wait. It will also be the case that s2 will try to lock s1 but this will also be locked and so it will also wait and hence a deadlock occurs. Note a thread can hold more than one lock at a time and synchronized statements can be nested (Dan Chisholm Exam - Dec20/Section7/Exam1/Question9).
-Iain
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a standard "deadlock" example.
Remember that Threads can switch in and out depending on the system.
So lets examine your example more closely...
Assume that Thread one starts first:
Thread (thread 1) first of all attempts to get a lock on object s1. Say for now this happens and the append happens. Now this thread is switched out by the operating system.
Thread 2 now comes along and grabs the lock for object s2 and does it's append. Now it attempts to get the lock for object s1. As thread 1 has this, it can't do anything so it goes into a waiting state.
Thread 1 is now switched back in by the operating system and tries to get the lock on object s2. unfortunately Thread 2 already has this lock, so it goes into a waiting state.
Now both threads are waiting for each other to give up their locks. This will continue forever! DEADLOCK.
Hope this helps,
Steve.
 
Richard Yi
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Iain and Stephen!
reply
    Bookmark Topic Watch Topic
  • New Topic