Comment utiliser les attendre et de les informer en Java sans IllegalMonitorStateException?

J'ai 2 matrices et j'ai besoin de les multiplier et de les imprimer ensuite les résultats de chaque cellule. Dès qu'une cellule est prête j'ai besoin de l'imprimer, mais par exemple j'ai besoin d'imprimer le [0][0] cellule avant de la cellule [2][0] même si le résultat de [2][0] est prêt le premier. J'ai donc besoin d'imprimer en ordre.
Donc, mon idée est de faire de l'imprimante de l'attente du thread jusqu'à ce que le multiplyThread informe que la bonne cellule est prêt à être imprimé, puis le printerThread impression sera de la cellule et de revenir à l'attente et ainsi de suite..

J'ai donc ce thread qui effectue la multiplication:

public void run() 
{
    int countNumOfActions = 0; //How many multiplications have we done
    int maxActions = randomize(); //Maximum number of actions allowed

    for (int i = 0; i < size; i++)
    {       
        result[rowNum][colNum] = result[rowNum][colNum] + row[i] * col[i];
        countNumOfActions++;
        //Reached the number of allowed actions
        if (countNumOfActions >= maxActions)
        {
            countNumOfActions = 0;
            maxActions = randomize();
            yield();
        }   
    }
    isFinished[rowNum][colNum] = true;
    notify();
}

Thread qui imprime le résultat de chaque cellule:

public void run()
{
    int j = 0; //Columns counter
    int i = 0; //Rows counter
    System.out.println("The result matrix of the multiplication is:");

    while (i < creator.getmThreads().length)
    {
        synchronized (this)
        {
            try 
            {
                this.wait();
            } 
            catch (InterruptedException e1) 
            {
            }
        }
        if (creator.getmThreads()[i][j].getIsFinished()[i][j] == true)
        {
            if (j < creator.getmThreads()[i].length)
            {
                System.out.print(creator.getResult()[i][j] + " ");
                j++;
            }
            else
            {
                System.out.println();
                j = 0;
                i++;
                System.out.print(creator.getResult()[i][j] + " ");
            }
        }
    }

Maintenant, il me jette ces exceptions:

Exception in thread "Thread-9" java.lang.IllegalMonitorStateException
at java.lang.Object.notify(Native Method)
at multiplyThread.run(multiplyThread.java:49)
Exception in thread "Thread-6" Exception in thread "Thread-4" java.lang.IllegalMonitorStateException
at java.lang.Object.notify(Native Method)
at multiplyThread.run(multiplyThread.java:49)
java.lang.IllegalMonitorStateException
at java.lang.Object.notify(Native Method)
at multiplyThread.run(multiplyThread.java:49)
Exception in thread "Thread-5" java.lang.IllegalMonitorStateException
at java.lang.Object.notify(Native Method)
at multiplyThread.run(multiplyThread.java:49)
Exception in thread "Thread-8" java.lang.IllegalMonitorStateException
at java.lang.Object.notify(Native Method)
at multiplyThread.run(multiplyThread.java:49)
Exception in thread "Thread-7" java.lang.IllegalMonitorStateException
at java.lang.Object.notify(Native Method)
at multiplyThread.run(multiplyThread.java:49)
Exception in thread "Thread-11" java.lang.IllegalMonitorStateException
at java.lang.Object.notify(Native Method)
at multiplyThread.run(multiplyThread.java:49)
Exception in thread "Thread-10" java.lang.IllegalMonitorStateException
at java.lang.Object.notify(Native Method)
at multiplyThread.run(multiplyThread.java:49)
Exception in thread "Thread-12" java.lang.IllegalMonitorStateException
at java.lang.Object.notify(Native Method)
at multiplyThread.run(multiplyThread.java:49)

ligne 49 dans multiplyThread est le "notify()"..je pense que j'ai besoin d'utiliser la synchronisation différemment, mais je ne suis pas sûr de savoir comment.

Si quelqu'un peut aider ce code fonctionne, je l'apprécie vraiment.

InformationsquelleAutor | 2009-05-20