SORU
20 Mayıs 2009, ÇARŞAMBA


Nasıl bekle kullanın ve Java ile bildirmek için?

2 matris var ve onlara çarpın ve sonra her hücre sonuçlarını yazdırın. En kısa sürede bir hücre hazır istiyorum baskı değil, ama örneğin ihtiyacım var yazdırmak için [0][0] önce hücre hücre [2][0] bile, sonuç [2][0] hazır ilk. Emri yazdırmak istiyorum. Bu yüzden benim fikrim olduğu için yazıcı iş parçacığı bekleyin multiplyThread bildirir bu doğru hücresi hazır basılmış ve daha sonra printerThread yazdırma hücre ve geri dönmek için bekleyen ve

Bu çarpma mı bu konu var:

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();
}

Her hücre sonucu yazdıran parçacığı:

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]   " ");
            }
        }
    }

Şimdi bana bu özel durumlar atar:

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)

multiplyThread 49 çizgidir "() bildir"..senkronize farklı kullanmak lazım diye düşünüyorum ama nasıl emin değilim.

Eğer bu kod işe yardımcı olabilir eğer çok minnettar olacağım.

CEVAP
20 Mayıs 2009, ÇARŞAMBA


notify() Ara edebilmek için, aynı nesne üzerinde senkronize etmek gerekir.

synchronized (someObject) {
    someObject.wait();
}

/* different thread / object */
synchronized (someObject) {
    someObject.notify();
}

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Official Android Tips

    Official And

    23 EYLÜL 2009
  • Austin Evans

    Austin Evans

    5 AĞUSTOS 2007
  • Besnik Ibrahimi

    Besnik Ibrah

    27 Mart 2010