Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How i can do this ?  RSS feed

 
John Lincoln
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
I have situation like this:
I have two classes Class1 and Class 2;
Class1 {
public Class1(){
}
private class insideclass1{
//methods
}
}
Class2 {
private class mythread extends Thread{
public void run(){
//logic
}
}
In Class2 , i create threads of type mythread and start them. These threads (logic) access synchronized methods in Class1 and should wait for notify() from class1.
My problem : In order to access methods in Class1, i have declared them to be synchronized and static
But i cannot call wait() and notify()and these are non-static methods.
How can over come this problem
Any help is apprciated .
Please help.
Thanks
Praveen.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static synchronized methods acquire a lock on the class object, so you can call Class1.class.wait() and so forth.
Whether this is a good design is another matter. Can't you pass a Class1 instance you can use, or alternatively, can't you turn Class1 into a Singleton? In a pinch, you could also consider a separate monitor object (an Object created just to synchronize on).
- Peter
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!