aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes wait() is block object pls help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "wait() is block object pls help" Watch "wait() is block object pls help" New topic
Author

wait() is block object pls help

Weerawit Maneepongsawat
Ranch Hand

Joined: Apr 11, 2002
Posts: 203
i'm confuse again
class ThreadTestMain {
public static void main(String[] args) {
ObjectValue objectValue = new ObjectValue();
ThreadTest1 test1 = new ThreadTest1(objectValue);
ThreadTest2 test2 = new ThreadTest2(objectValue);
test1.start();
test2.start();
}
}
class ThreadTest1 extends Thread {
private ObjectValue v;
public ThreadTest1(ObjectValue a) {
this.v = a;
}
public synchronized void run() {
while (true) {
try {
v.wait();
System.out.println("v.wait()");
} catch (Exception e) {}
}
}
};
class ThreadTest2 extends Thread {
private ObjectValue v;
public ThreadTest2(ObjectValue a) {
this.v = a;
}
public void run() {
while (true) {
try {
System.out.println(v.getABC());
} catch (Exception e) {}
}
}
}
class ObjectValue {
private String abc = "ABC";
public String getABC() {
return abc;
}
}
at v.wait(); is mean v is block, it can't access by another thread, this is true??
is there so many thread in exam right??
i don't understand this, wait(), notify(), notifyAll();
pls help me for SCJP...thank


Weerawit<br />SCJP 1.4, SCWCD 1.2, SCWCD 1.4, SCBCD 1.3, SCBCD 5.0, SCMAD 1.0, SCJA 1.0, SCDJWS, SCJD, SCEA, SCDJWS 1.5
Weerawit Maneepongsawat
Ranch Hand

Joined: Apr 11, 2002
Posts: 203
sorry for my english...i hope u understand..
mohamed hamdy
Ranch Hand

Joined: Feb 13, 2003
Posts: 72
could you tell if this is a question in a mock exam, and if so what are the suggested answers?
in my view the call v.wait() will do nothing, since the wait must be called in a synchronized method of some class, so that any thread that call this method through a reference to an object of this class will release the key i.e the thread will be blocked (not the object) so that any other thread try to call any other synchronized method of that class using a referrence to the same object (not other object even if it is of same class) will have the chance to pick the key of the object.
the thread that was blocked will not have the chance to be runnable again except if another thread calls notifyAll() or notify() .
Of course the call to one of this methods must be in any of the synchronized methods of that class (using a reference to the the same object).
notifyAll will awake all waiting threads.
notify will awake one of the waiting threads at random.
mohamed hamdy
Ranch Hand

Joined: Feb 13, 2003
Posts: 72
another notice what i sayed rely on that the synchrized methods are instance members of the calss.
Weerawit Maneepongsawat
Ranch Hand

Joined: Apr 11, 2002
Posts: 203
mohamed hamdy ..
sorry i'm a beginner of java
just one question... is different between v.wait(); and wait(); in method run than you
mohamed hamdy
Ranch Hand

Joined: Feb 13, 2003
Posts: 72
v.wait() or wait() will throw IllegalMonitorStateException : current thread isn't the owner.
in my view it mean that for the current thread to be the owner of the object it must reach the call of wait() in a synchronized method of that object.
look to thus code:
public class test{
MyThread t1,t2;
synchronized void me(){
if(Thread.currentThread==t1){
System.out.println("t1 is the owner");
}
if(Thread.currentThread==t2){
System.out.println("t2 is the owner");
}
// you can call wait() here in some
// condition so that the current thread
// is blocked
}
public static void main(String[] args){
test ob=new test();
t1=new MyThread(ob);
t2=new MyThread(ob);
t1.start();
t2.start();
}
}
class MyThread extends Thread{
test v;
MyThread(test v){
this.v=v;
}
public void run(){
for(int i=0;i<20;i++){
v.me();
}
}
}
mohamed hamdy
Ranch Hand

Joined: Feb 13, 2003
Posts: 72
sorry curentThread() is a method
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: wait() is block object pls help