aspose file tools*
The moose likes Java in General and the fly likes Why notify is throwing an exception here ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Why notify is throwing an exception here ?" Watch "Why notify is throwing an exception here ?" New topic
Author

Why notify is throwing an exception here ?

Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

Hi, I am writing a code, In which I am having two 3 GB text files and merging them onto one 6 GB file. I am trying to implement the same via Multi threading. Here is my Code :


This code is throwing following Exception :


I don't know why notify is Throwing an Exception, I got the lock on the Object lock, Then why ?


Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

The instance used for synchronization and notify/notifyAll must be the same.
In your code you are synchronizing on lock, but calling notify() on instances of the class FileRead/FileWrite.


apigee, a better way to API!
Daniel Chemko
Ranch Hand

Joined: Feb 27, 2008
Posts: 32
I didn't spend much time skimming your code, but I know that notify() doesn't have 'lock.' prefixing it, so you're trying to notify() against the ReadThread instead of the lock.
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

Sorry Guys I still don't understand, I am using one instance of class ABCD to initiate two threads. Both of Thread have their own run methods or you can say own stack. They are using an Object belongs to parent class.
In one Thread i m getting lock on that Object and then calling wait on that Object, means i release the lock on that Object. In another thread I get the lock on that Object and then call notify . This certainly means that I am calling notify for "lock" Object. I don't understand when you said I am calling notify on FileRead thread?

Can you please help me to clarify that?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[SJ]: I don't understand when you said I am calling notify on FileRead thread?

Look at the code:

Calling notify() is the same as calling this.notify(), where "this" refers to the current instance of the current class, which is FileRead. Since that's a different object than the one you synchronized on, you get an exception.
[ March 04, 2008: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

Originally posted by Jim Yingst:
[SJ]: I don't understand when you said I am calling notify on FileRead thread?

Look at the code:

Calling notify() is the same as calling this.notify(), where "this" refers to the current instance of the current class, which is FileRead. Since that's a different object than the one you synchronized on, you get an exception.

[ March 04, 2008: Message edited by: Jim Yingst ]


I agree with you Jim but If you look into my code, I am doing this:



I am calling notify with in synchronized block, also the sync object is "lock", Do you mean in my case even though i am calling notify with in synchronized block but still this.notify() is being called..? If yes, then i dont understand how can this possible..!! Please help me..!!
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[SJ]: I am calling notify with in synchronized block, also the sync object is "lock", Do you mean in my case even though i am calling notify with in synchronized block but still this.notify() is being called..?

Yes.

[SJ]: If yes, then i dont understand how can this possible..!! Please help me..!!

Well, that's true for any instance method. If you write "foo()" it's equivalent to calling this.foo(). That doesn't change just because you're using synchronization here. You're still inside the FileRead class, and so "this" refers to an instance of FileRead.

You need to write the code so that the synchronization and the notify() use the same object. Either use synchronized(this) and this.notify(), or use synchronized(lock) and lock.notify(). I think the latter makes much more sense here. But "notify()" won't mean lock.notify() just because you want it to - you need to write lock.notify() in order to notify using the lock object. That's what the compiler understands.
[ March 06, 2008: Message edited by: Jim Yingst ]
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

Thanks Jim..!! Now I got it..Thanks a lot..!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why notify is throwing an exception here ?