主题:大家帮我看看这段代码问题出在哪里?
[size=3]import java.util.*;
class Pair{
private int x,y;
public Pair(int x,int y){
this.x = x;
this.y = y;
}
public Pair(){ this(0,0);}
public int getX(){return x;}
public int getY(){return y;}
public void incrementX(){x++;}
public void incrementY(){y++;}
public String tostring(){
return "x:"+ x +", y:"+y;
}
class PairValuesNotEqualException extends RuntimeException{
public PairValuesNotEqualException(){
super("这一对值不相等:"+ Pair.this );
}
}
public void checkstate(){
if(x!=y)
throw new PairValuesNotEqualException();
}
}
abstract class PairManager{
protected Pair p = new Pair();
private List storage=new ArrayList();
public synchronized Pair getPair(){
return new Pair(p.getX(),p.getY());
}
protected void store(){storage.add(getPair());
}
public abstract void dotask();
}
class PairManager1 extends PairManager{
public synchronized void dotask(){
p.incrementX();
p.incrementY();
store();
}
}
class PairManager2 extends PairManager{
public void dotask(){
synchronized(this){
p.incrementX();
p.incrementY();
}
store();
}
}
class PairManipulator extends Thread{
private PairManager pm;
private int checkCounter=0;
private class PairChecker extends Thread{
PairChecker(){start();}
public void run(){
while(true){
checkCounter++;
pm.getPair().checkstate();
}
}
}
public PairManipulator(PairManager pm){
this.pm=pm;
start();
new PairChecker();
}
public void run(){
while(true){
pm.dotask();
}
}
public String toString(){
return"一对数:"+pm.getPair()+"检查="+checkCounter;
}
}
public class CriticalSection {
public static void main(String[] args) {
final PairManipulator
pm1 = new PairManipulator(new PairManager1()),
pm2 = new PairManipulator(new PairManager2());
new Timer(true).schedule(new TimerTask(){
public void run(){
System.out.println("pm1:"+ pm1);
System.out.println("pm2:"+ pm2);
System.exit(0);
}
},500);
}
}
以上这段代码是书上的一个例子,可是我运行了好多次都出错了,每次都出现以下提示,请大家帮我看看究竟是什么问题呢?
Exception in thread "Thread-0" java.lang.NoSuchMethodError: Pair.incrementX()V
at PairManager1.dotask(CriticalSection.java:39)
at PairManipulator.run(CriticalSection.java:72)
Exception in thread "Thread-1" java.lang.NoSuchMethodError: Pair.getX()I
at PairManager.getPair(CriticalSection.java:31)
at PairManipulator$PairChecker.run(CriticalSection.java:61)
Exception in thread "Thread-2" java.lang.NoSuchMethodError: Pair.incrementX()V
at PairManager2.dotask(CriticalSection.java:47)
at PairManipulator.run(CriticalSection.java:72)
Exception in thread "Thread-3" java.lang.NoSuchMethodError: Pair.getX()I
at PairManager.getPair(CriticalSection.java:31)
at PairManipulator$PairChecker.run(CriticalSection.java:61)[/size]
class Pair{
private int x,y;
public Pair(int x,int y){
this.x = x;
this.y = y;
}
public Pair(){ this(0,0);}
public int getX(){return x;}
public int getY(){return y;}
public void incrementX(){x++;}
public void incrementY(){y++;}
public String tostring(){
return "x:"+ x +", y:"+y;
}
class PairValuesNotEqualException extends RuntimeException{
public PairValuesNotEqualException(){
super("这一对值不相等:"+ Pair.this );
}
}
public void checkstate(){
if(x!=y)
throw new PairValuesNotEqualException();
}
}
abstract class PairManager{
protected Pair p = new Pair();
private List storage=new ArrayList();
public synchronized Pair getPair(){
return new Pair(p.getX(),p.getY());
}
protected void store(){storage.add(getPair());
}
public abstract void dotask();
}
class PairManager1 extends PairManager{
public synchronized void dotask(){
p.incrementX();
p.incrementY();
store();
}
}
class PairManager2 extends PairManager{
public void dotask(){
synchronized(this){
p.incrementX();
p.incrementY();
}
store();
}
}
class PairManipulator extends Thread{
private PairManager pm;
private int checkCounter=0;
private class PairChecker extends Thread{
PairChecker(){start();}
public void run(){
while(true){
checkCounter++;
pm.getPair().checkstate();
}
}
}
public PairManipulator(PairManager pm){
this.pm=pm;
start();
new PairChecker();
}
public void run(){
while(true){
pm.dotask();
}
}
public String toString(){
return"一对数:"+pm.getPair()+"检查="+checkCounter;
}
}
public class CriticalSection {
public static void main(String[] args) {
final PairManipulator
pm1 = new PairManipulator(new PairManager1()),
pm2 = new PairManipulator(new PairManager2());
new Timer(true).schedule(new TimerTask(){
public void run(){
System.out.println("pm1:"+ pm1);
System.out.println("pm2:"+ pm2);
System.exit(0);
}
},500);
}
}
以上这段代码是书上的一个例子,可是我运行了好多次都出错了,每次都出现以下提示,请大家帮我看看究竟是什么问题呢?
Exception in thread "Thread-0" java.lang.NoSuchMethodError: Pair.incrementX()V
at PairManager1.dotask(CriticalSection.java:39)
at PairManipulator.run(CriticalSection.java:72)
Exception in thread "Thread-1" java.lang.NoSuchMethodError: Pair.getX()I
at PairManager.getPair(CriticalSection.java:31)
at PairManipulator$PairChecker.run(CriticalSection.java:61)
Exception in thread "Thread-2" java.lang.NoSuchMethodError: Pair.incrementX()V
at PairManager2.dotask(CriticalSection.java:47)
at PairManipulator.run(CriticalSection.java:72)
Exception in thread "Thread-3" java.lang.NoSuchMethodError: Pair.getX()I
at PairManager.getPair(CriticalSection.java:31)
at PairManipulator$PairChecker.run(CriticalSection.java:61)[/size]