Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Why notify is throwing an exception here ?

 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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 ]
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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 ]
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jim..!! Now I got it..Thanks a lot..!!
 
She'll be back. I'm just gonna wait here. With this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic