L
lonelyplanet999
Hi,
I have 2 java programs doing similar things:
==============================================================================
Program 1 below successfully achieved synchronization among 3 created
threads such that the first printed 100 'A' before the second printed
100 'B' followed by the third printed 100 'C'
==============================================================================
class InSync extends Thread {
StringBuffer letter;
public InSync(StringBuffer letter) {
this.letter = letter;
}
public void run() {
synchronized(letter) {
for (int i=1; i<=100; i++) {
System.out.print(letter);
}
System.out.println();
char temp = letter.charAt(0);
++temp;
letter.setCharAt(0,temp);
}
}
public static void main (String [] args) {
StringBuffer sb = new StringBuffer("A");
new InSync(sb).start();
new InSync(sb).start();
new InSync(sb).start();
}
}
==============================================================================
Program 2 below aims to achieve the same function. However, the output
was not expected.
This program outputted below when ran.
Thread-1
AAAAAAAAAThread-3
AAAThread-2
AAAAAAAAAAA...... (80 'A's in one row)
AAAAAAAAAAA...... (80 'A's in one row)
AAAAAAAAAAA...... (80 'A's in one row)
AAAAAA..... (38 'A's in one row)
AACheck c=A
AAAA.... (17 'A's in one row)
ACheck c=A
Check c=A
Obviously, the code couldn't synchronize execution of the three
threads. I couldn't understand what's the difference between program 1
& 2 that caused failure of program 2 to outputted what's outputted by
program 1 ?
Tks ;\
==============================================================================
class Ch9ex2 extends Thread {
private StringBuffer sb = new StringBuffer("A");
public static void main (String [] args) {
Ch9ex2 mt1 = new Ch9ex2();
Ch9ex2 mt2 = new Ch9ex2();
Ch9ex2 mt3 = new Ch9ex2();
mt1.start();
mt2.start();
mt3.start();
}
public void run() {
System.out.println(Thread.currentThread().getName());
synchronized(sb) {
for (int i=0; i<100; i++) {
System.out.print(sb);
}
System.out.println("");
char c = sb.charAt(0);
System.out.println("Check c="+c);
c += 1;
sb.setCharAt(0,c);
}
}
}
I have 2 java programs doing similar things:
==============================================================================
Program 1 below successfully achieved synchronization among 3 created
threads such that the first printed 100 'A' before the second printed
100 'B' followed by the third printed 100 'C'
==============================================================================
class InSync extends Thread {
StringBuffer letter;
public InSync(StringBuffer letter) {
this.letter = letter;
}
public void run() {
synchronized(letter) {
for (int i=1; i<=100; i++) {
System.out.print(letter);
}
System.out.println();
char temp = letter.charAt(0);
++temp;
letter.setCharAt(0,temp);
}
}
public static void main (String [] args) {
StringBuffer sb = new StringBuffer("A");
new InSync(sb).start();
new InSync(sb).start();
new InSync(sb).start();
}
}
==============================================================================
Program 2 below aims to achieve the same function. However, the output
was not expected.
This program outputted below when ran.
Thread-1
AAAAAAAAAThread-3
AAAThread-2
AAAAAAAAAAA...... (80 'A's in one row)
AAAAAAAAAAA...... (80 'A's in one row)
AAAAAAAAAAA...... (80 'A's in one row)
AAAAAA..... (38 'A's in one row)
AACheck c=A
AAAA.... (17 'A's in one row)
ACheck c=A
Check c=A
Obviously, the code couldn't synchronize execution of the three
threads. I couldn't understand what's the difference between program 1
& 2 that caused failure of program 2 to outputted what's outputted by
program 1 ?
Tks ;\
==============================================================================
class Ch9ex2 extends Thread {
private StringBuffer sb = new StringBuffer("A");
public static void main (String [] args) {
Ch9ex2 mt1 = new Ch9ex2();
Ch9ex2 mt2 = new Ch9ex2();
Ch9ex2 mt3 = new Ch9ex2();
mt1.start();
mt2.start();
mt3.start();
}
public void run() {
System.out.println(Thread.currentThread().getName());
synchronized(sb) {
for (int i=0; i<100; i++) {
System.out.print(sb);
}
System.out.println("");
char c = sb.charAt(0);
System.out.println("Check c="+c);
c += 1;
sb.setCharAt(0,c);
}
}
}