日期:2014-05-20 浏览次数:20894 次
public class Test{
public static void main(String[ ] args) {
ThreadTest a = new ThreadTest();
for(int i=0;i<100;i++){
new Thread(a).start();
a.print();
}
}
}
class ThreadTest implements Runnable {
Count c = new Count();
@Override
public void run() {
c.increase();
}
public void print() {
System.out.println(c.getValue());
}
}
final class Count {
private long value = 0;
public synchronized long getValue() {
return value;
}
public synchronized long increase() {
if(value == Long.MAX_VALUE) {
throw new IllegalStateException("error");
}
return ++value;
}
}
public class Test22{
public static void main(String[ ] args) {
ThreadTest a = new ThreadTest();
for(int i=0;i<100;i++){
new Thread(a).start();
// a.print();
}
}
}
class ThreadTest implements Runnable {
Count c = new Count();
public void run() {
c.increase();
}
// public void print() {
// System.out.println(c.getValue());
// }
}
final class Count {
private long value = 0;
public synchronized long getValue() {
return value;
}
public synchronized long increase() {
if(value == Long.MAX_VALUE) {
throw new IllegalStateException("error");
}
System.out.println(++value);