A
Archimede
Hi all
I got two diffrent version of java file:
public class TreadInConstructor implements Runnable {
private static int cnt = 0;
public TreadInConstructor() {
Thread.yield();
cnt++;
}
public void run() {
try {
Thread.sleep(30);
new TreadInConstructor();
} catch (Exception e) {}
return;
}
public static void main(String[] args) throws Exception {
int millis = 10000;
TreadInConstructor tic = new TreadInConstructor();
for(int i = 0 ; i < 1000; i++) new Thread(tic).start();
Thread.sleep(millis);
System.out.println("cnt = " + cnt);
}
}
and the second one
public class TreadInConstructor2 implements Runnable {
volatile private static int cnt = 0;
public TreadInConstructor2() {
synchronized (TreadInConstructor2.class) {
Thread.yield();
cnt++;
}
}
public void run() {
new TreadInConstructor2();
return;
}
public static void main(String[] args) throws Exception {
int millis = 10000;
TreadInConstructor2 tic = new TreadInConstructor2();
for(int i = 0 ; i < 10000; i++) new Thread(tic).start();
Thread.sleep(millis);
System.out.println("cnt = " + cnt);
}
}
Now I'm expecting that the synchronized block in the constructor will
accomodate any conflicting access to the static cnt member and the
version without any synchronization will not run right
(that is the program won't print 10001).
The point is that I don't see any differences.
In a dual processor pc will they run without any differences?
If so why?
Thanx in advance
I got two diffrent version of java file:
public class TreadInConstructor implements Runnable {
private static int cnt = 0;
public TreadInConstructor() {
Thread.yield();
cnt++;
}
public void run() {
try {
Thread.sleep(30);
new TreadInConstructor();
} catch (Exception e) {}
return;
}
public static void main(String[] args) throws Exception {
int millis = 10000;
TreadInConstructor tic = new TreadInConstructor();
for(int i = 0 ; i < 1000; i++) new Thread(tic).start();
Thread.sleep(millis);
System.out.println("cnt = " + cnt);
}
}
and the second one
public class TreadInConstructor2 implements Runnable {
volatile private static int cnt = 0;
public TreadInConstructor2() {
synchronized (TreadInConstructor2.class) {
Thread.yield();
cnt++;
}
}
public void run() {
new TreadInConstructor2();
return;
}
public static void main(String[] args) throws Exception {
int millis = 10000;
TreadInConstructor2 tic = new TreadInConstructor2();
for(int i = 0 ; i < 10000; i++) new Thread(tic).start();
Thread.sleep(millis);
System.out.println("cnt = " + cnt);
}
}
Now I'm expecting that the synchronized block in the constructor will
accomodate any conflicting access to the static cnt member and the
version without any synchronization will not run right
(that is the program won't print 10001).
The point is that I don't see any differences.
In a dual processor pc will they run without any differences?
If so why?
Thanx in advance