• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

more fun Qestions, thread=)

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


������14.public class Syntest
public static void main(String []args)
final StringBuffer s1=new StringBuffer();
final StringBuffer s2=new StringBuffer();
new Thread()
public void run()
synchronized(s1)
s1.append("a");
synchronized("b")
System.out.println(s1);
System.out.println(s2);
.start();//not error
new Thread()
public void run()
synchronized(s2)
s2.append("c");
synchronized(s1)
s1.append("d");
System.out.println(s2);
System.out.println(s1);

.start();

output?
a)print ABBCAD
b)print CDDACB
c)print ADCBADBC
d)The output is a not-deterministic point because of a
possible deadlock condition
e)The output is dependent on the threading model of
the ysstem the program is running on.
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think ansewr is d and e,since there is possibility of dead lock.
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin, is this how the question appears? (No indents, no braces?) Without braces, how can we figure this out?

is not the same as


[This message has been edited by Scott Appleton (edited June 20, 2001).]
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//program1
public class ThreadTest {
public static void main(String []args) {
final StringBuffer s1=new StringBuffer();
final StringBuffer s2=new StringBuffer();
new Thread() { //anynomous class
public void run() {
synchronized(s1) {
s1.append("a");
}
synchronized(s2) {
s2.append("b");
}
System.out.println(s1);
System.out.println(s2);
}
}.start();

new Thread() {//anynomous class
public void run() {
synchronized(s2) {
s2.append("c");
}
synchronized(s1) {
s1.append("d");
}
System.out.println(s2);
System.out.println(s1);
}
}.start();
}
}
For program1, deadlock will never happen, but the output is dependent on the threading model of the sysstem the program is running on.
//program2
public class ThreadTest{
public static void main(String []args) {
final StringBuffer s1=new StringBuffer();
final StringBuffer s2=new StringBuffer();
new Thread() {
public void run() {
synchronized(s1) {
s1.append("a");
synchronized(s2) {
s2.append("b");
}
}
System.out.println(s1);
System.out.println(s2);
}
}.start();

new Thread() {
public void run() {
synchronized(s2) {
s2.append("c");
synchronized(s1) {
s1.append("d");
}
}
System.out.println(s2);
System.out.println(s1);
}
}.start();
}
}
For program2, the output is a not-deterministic point because of a possible deadlock condition although it run well on my machine.
output:
a
b
bc
ad
Scott, am I right?
 
Scott Appleton
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bin, I think you've nailed it. Modify the second program so that new Threads are continuously created (possibly with varying delays between creations) and you will likely reach deadlock sooner or later. The first program in your post shouldn't become deadlocked.
The output of neither program is predictable.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic