IntelliJ Java IDE
The moose likes Threads and Synchronization and the fly likes How i can do this ? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "How i can do this ?" Watch "How i can do this ?" New topic
Author

How i can do this ?

John Lincoln
Ranch Hand

Joined: Feb 11, 2003
Posts: 192
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

Joined: Apr 20, 2000
Posts: 3252
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
 
 
subject: How i can do this ?
 
Threads others viewed
How to access the methods in interface(parameters user defined) using wsdd.
Accessing component from main class.... Help!
calling a method
does inheritance break the encapsulation or data hiding?
not able to access static data
IntelliJ Java IDE

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture