Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java wait and notifyAll:IllegalMonitorStateException_Java_Multithreading_Thread Safety_Synchronize - Fatal编程技术网

Java wait and notifyAll:IllegalMonitorStateException

Java wait and notifyAll:IllegalMonitorStateException,java,multithreading,thread-safety,synchronize,Java,Multithreading,Thread Safety,Synchronize,我是Java新手(和RoR开发者) 我有一个简单的程序。 球由许多运动员共用。球应该传给随机球员 好的,代码如下: class Ball { private int currentPlayer; public void setCurrentPlayer( int currentPlayer, int fromWho ) { this.currentPlayer = currentPlayer; System.out.println( "Ball

我是Java新手(和RoR开发者)

我有一个简单的程序。 球由许多运动员共用。球应该传给随机球员

好的,代码如下:

class Ball  {
    private int currentPlayer;

    public void setCurrentPlayer( int currentPlayer, int fromWho ) {
        this.currentPlayer = currentPlayer;
        System.out.println( "Ball:setCurrentPlayer " + fromWho + " ---> " + currentPlayer );
    }

    public int getCurrentPlayer() {
        return currentPlayer;
    }
}

class Player implements Runnable {
    private int myID;
    private Ball ball;
    private int playersCount;
    java.util.Random rnd;

    public Player(int id, Ball ball, int playersCount) {
        myID = id;
        this.ball = ball;
        this.playersCount = playersCount;
        rnd = new java.util.Random( id );
    }

    public void run() {
        int nextPlayer;
        while (true) {
            synchronized (ball) {
                if ( ball.getCurrentPlayer() == myID ) {
                    nextPlayer = rnd.nextInt(playersCount);
                    System.out.println( "Player nr. " + myID + " ---> " + nextPlayer );
                    ball.setCurrentPlayer( nextPlayer, myID );
                    ball.notifyAll();
                }  else {
                    try {
                        wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}

class Start {
    public static void main( String[] argv ) throws Exception {
        Ball p = new Ball();
        System.out.println("MAIN: ball will be in player: " + p.getCurrentPlayer());

        final int playersCount = 5;

        for ( int i = 0; i < playersCount; i++ ) {
            ( new Thread( new Player( i, p, playersCount ) ) ).start();
        }

        while ( true ) {
            Thread.sleep( 500 );
            System.out.println( "MAIN: ball is in player : " + p.getCurrentPlayer() );
        }
    }
}
班级舞会{
私人球员;
public void setCurrentPlayer(int currentPlayer,int fromWho){
this.currentPlayer=currentPlayer;
System.out.println(“Ball:setCurrentPlayer”+fromWho+“-->”+currentPlayer);
}
public int getCurrentPlayer(){
返回当前播放器;
}
}
类播放器实现Runnable{
私有int-myID;
私人舞会;
私人int玩家计数;
java.util.Random rnd;
公众球员(国际球员id、球、国际球员计数){
myID=id;
这个球=球;
this.playersCount=playersCount;
rnd=newjava.util.Random(id);
}
公开募捐{
int nextPlayer;
while(true){
同步(球){
if(ball.getCurrentPlayer()==myID){
nextPlayer=rnd.nextInt(playersCount);
System.out.println(“播放器编号”+myID+“-->”+nextPlayer);
ball.setCurrentPlayer(nextPlayer,myID);
ball.notifyAll();
}否则{
试一试{
等待();
}捕捉(中断异常e){
e、 printStackTrace();
}
}
}
}
}
}
开课{
公共静态void main(字符串[]argv)引发异常{
球p=新球();
System.out.println(“主:球将在球员:”+p.getCurrentPlayer());
最终整数playersCount=5;
对于(int i=0;i
但它不起作用。 我得到异常:
IllegalMonitorStateException


如何修复此问题?

您正在等待
监视器,但尚未对其进行同步;您需要等待
ball
,而不是您正在等待
监视器,而没有在其上进行同步;你需要等球
而不是

天啊,我注意到了谢谢你的快速回复!我会在10分钟内接受这个答案……天哪,我注意到了谢谢你的快速回复!我将在10分钟内接受这个答案……这个异常的Javadoc解释了它的含义。阅读文档可以解决很多问题;)这个异常的Javadoc解释了它的含义。阅读文档可以解决很多问题;)