Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

THREAD

 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Source: Devaka Cooray's Exam Simulator.




Why is main not completing its execution??
What does Line1 signify?
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried to run it because i'm getting a java.lang.IllegalMonitorStateException.
That's because the notify statement must be in synchronized code.

Read about interrupts here
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok..that means the Thread refernced by t is never notified, so it will never terminate.

But a.interrupt() dosent make any difference right?
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Main is terminating but the thread you have started that is not terminating.
As thread t has called wait on object of A means on a.


 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The program is terminated because an exception is thrown.
Try putting the notifyAll in a synchronized block.
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can t be notified???

synchronized(this)// dosent work inside main, so what should be done.

I know its a silly one..but cant figure it out.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
synchronized(t)
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Do this.
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks..wouter.
Actually i get confused with Thread questions to such an extent that I actually forget Thread basics.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Problem is here also Abhi, you have to synchronize on this also, to run it perfectly.


And than you have to modify main also.


a.interrupt(); will do nothing. As thread running is referenced by thread t. It is just a tricky part to confuse you.
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks . punit.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic