Java 如何在通过循环修改对象中的数据时立即访问该对象中的数据

Java 如何在通过循环修改对象中的数据时立即访问该对象中的数据,java,multithreading,loops,field,Java,Multithreading,Loops,Field,这是密码 public class Main { public static void main(String[] args) throws InterruptedException { // A running ball class Ball { private double x, y, radius, xvel, yvel; private long timeframe; pri

这是密码

public class Main {
    public static void main(String[] args) throws InterruptedException {

        // A running ball
        class Ball {
            private double x, y, radius, xvel, yvel;
            private long timeframe;
            private boolean moving;
            public Ball(double x, double y, double radius, double xvel, double yvel, long timeframe) throws InterruptedException {
                super();
                this.x = x;
                this.y = y;
                this.radius = radius;
                this.xvel = xvel;
                this.yvel = yvel;
                this.timeframe = timeframe;
                this.moving = true;
                while(moving) {
                    // The next print line is what i want to run outside of this constructor
                    System.out.println("x:" + getX() + "   " + "y:" + getY());
                    move();
                    Thread.sleep(timeframe);
                }
            }
            private void move() {
                x += xvel;
                y += yvel;
            }
            public double getX() {
                return x;
            }
            public double getY() {
                return y;
            }
            public void setXvel(double xvel) {
                this.xvel = xvel;
            }
            public void setYvel(double yvel) {
                this.yvel = yvel;
            }
        }

        Ball ball = new Ball(0, 0, 1, .5, .5, 1000);
    }
}
public class Main {
// a running ball
public class Ball extends Circle implements Runnable{
    private double xvel, yvel;
    private long timeframe;
    private boolean moving = true;

    public boolean isMoving() {
        return moving;
    }

    public void setMoving(boolean moving) {
        this.moving = moving;
    }

    public Ball(double x, double y, double radius, double xvel, double yvel, long timeframe) {
        super(new Point(x, y), radius);
        this.xvel = xvel;
        this.yvel = yvel;
        this.timeframe = timeframe;
        new Thread(this).start();
    }

    @Override
    public void run() {
        while(moving) {
            move(xvel, yvel);
            try {
                Thread.sleep(timeframe);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }       
    }

    public double getX() {
        return getCenter().getX();
    }

    public double getY() {
        return getCenter().getY();
    }

    public void setXvel(double xvel) {
        this.xvel = xvel;
    }
    public void setYvel(double yvel) {
        this.yvel = yvel;
    }
}

public static void main(String[] args) throws InterruptedException {

    // a Ball fields getter
    class Getter implements Runnable{
        Ball ball;
        public Getter(Ball ball) {
            this.ball = ball;
        }
        public void run() {
            while(ball.isMoving()) {
                System.out.println(ball.getX() + " " + ball.getY());                    
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        }

    }

    Ball ball = new Main().new Ball(0, 0, 1, .5, .5, 1000);
    Thread getter = new Thread(new Getter(ball));
    getter.start();

}
}
现在我想在球运动的同时进入球的x和y场 他正在跑步。我不能在这里做ball.getX(),因为 构造函数内部有一个循环 首先完成。 在循环运行时,如何执行此操作。 我有使用线程的想法,但我看不出来
实施细节。

晚上好,麦克·贾洛

我认为您要搜索的是synchronized Java关键字: 与Java 7中引入的Executor框架一起:

简言之,如果您希望球移动,并且如果外部观察者必须读取轨迹,则可能需要两个不同的线程,在访问同一对象时,这两个线程是同步的。事实上,在Java中,您不能修改球的位置,同时读取球。为什么?因为你永远无法确定职位何时更新。这会导致读写位置的不确定性(在google中查找线程安全性)。我们不喜欢不确定性,所以我们放了synchronized关键字(可以说是先到先得)


此外,在我看来,将球运动包含在球构造中不是一个好的做法。事实上,构造器回答了“什么是球(具有原点和半径的3D对象)”,而平移或旋转运动回答了运动的物理问题。这两个问题需要去关联(您可以引入一个作用于Ball实例的外部对象)

我想我终于找到了我所需要的解决方案。下面是代码

public class Main {
    public static void main(String[] args) throws InterruptedException {

        // A running ball
        class Ball {
            private double x, y, radius, xvel, yvel;
            private long timeframe;
            private boolean moving;
            public Ball(double x, double y, double radius, double xvel, double yvel, long timeframe) throws InterruptedException {
                super();
                this.x = x;
                this.y = y;
                this.radius = radius;
                this.xvel = xvel;
                this.yvel = yvel;
                this.timeframe = timeframe;
                this.moving = true;
                while(moving) {
                    // The next print line is what i want to run outside of this constructor
                    System.out.println("x:" + getX() + "   " + "y:" + getY());
                    move();
                    Thread.sleep(timeframe);
                }
            }
            private void move() {
                x += xvel;
                y += yvel;
            }
            public double getX() {
                return x;
            }
            public double getY() {
                return y;
            }
            public void setXvel(double xvel) {
                this.xvel = xvel;
            }
            public void setYvel(double yvel) {
                this.yvel = yvel;
            }
        }

        Ball ball = new Ball(0, 0, 1, .5, .5, 1000);
    }
}
public class Main {
// a running ball
public class Ball extends Circle implements Runnable{
    private double xvel, yvel;
    private long timeframe;
    private boolean moving = true;

    public boolean isMoving() {
        return moving;
    }

    public void setMoving(boolean moving) {
        this.moving = moving;
    }

    public Ball(double x, double y, double radius, double xvel, double yvel, long timeframe) {
        super(new Point(x, y), radius);
        this.xvel = xvel;
        this.yvel = yvel;
        this.timeframe = timeframe;
        new Thread(this).start();
    }

    @Override
    public void run() {
        while(moving) {
            move(xvel, yvel);
            try {
                Thread.sleep(timeframe);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }       
    }

    public double getX() {
        return getCenter().getX();
    }

    public double getY() {
        return getCenter().getY();
    }

    public void setXvel(double xvel) {
        this.xvel = xvel;
    }
    public void setYvel(double yvel) {
        this.yvel = yvel;
    }
}

public static void main(String[] args) throws InterruptedException {

    // a Ball fields getter
    class Getter implements Runnable{
        Ball ball;
        public Getter(Ball ball) {
            this.ball = ball;
        }
        public void run() {
            while(ball.isMoving()) {
                System.out.println(ball.getX() + " " + ball.getY());                    
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        }

    }

    Ball ball = new Main().new Ball(0, 0, 1, .5, .5, 1000);
    Thread getter = new Thread(new Getter(ball));
    getter.start();

}
}

我喜欢把移动的机械原理放在构造器中,因为它看起来更自然,相对于我正在建模的东西来说,这是一个移动的球,不需要外部演员来移动它

你的意思是如何实施有关财产变更的通知?在构造函数中有一个无限循环似乎很奇怪。您可能应该将该循环移动到名为
start
或类似的函数,然后在另一个线程中运行该循环。也许会有帮助。