views:

9107

answers:

4

Hello,

I have 2 matrices and I need to multiply them and then print the results of each cell. As soon as one cell is ready I need to print it, but for example I need to print the [0][0] cell before cell [2][0] even if the result of [2][0] is ready first. So I need to print it by order. So my idea is to make the printer thread wait until the multiplyThread notifies it that the correct cell is ready to be printed and then the printerThread will print the cell and go back to waiting and so on..

So I have this thread that does the multiplicaion:

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 that prints the result of each cell:

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

Now it throws me this exception:

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)

line 49 in multiplyThread is the "notify()"..I think I need to use the synchronized differently but I am not sure how.

If anyone can help this code to work I will really appriciate it.

Thanks in advance,

Greg

+1  A: 

notify() needs to be synchronized as well

takete.dk
+8  A: 

To be able to call notify() you need to synchronize on the same object.

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

/* different thread / object */
synchronized (someObject) {
    someObject.notify();
}
Bombe
+4  A: 

You can only call notify on objects where you own their monitor. So you need something like

synchronized(threadObject)
{
   threadObject.notify();
}
Visage
+5  A: 

Do you need to thread this at all ? I'm wondering how big your matrices are, and whether there's any benefit in having one thread print whilst the other does the multiplication.

Perhaps it would be worth measuring this time before doing the relatively complex threading work ?

If you do need to thread it, I would create 'n' threads to perform the multiplication of the cells (perhaps 'n' is the number of cores available to you), and then use the ExecutorService and Future mechanism to dispatch multiple multiplications simultaneously.

That way you can optimise the work based on the number of cores, and yu're using the higher level Java threading tools (which should make life easier). Write the results back into a receiving matrix, and then simply print this once all your Future tasks have completed.

Brian Agnew
+1 @Greg I think you should take a look at java.util.concurrent package, as pointed out by Brian.
ATorras
+1 and also check out this book which will also teach you the correct way to use wait() and notify() http://jcip.net/
Chii