• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

wait() is block object pls help

 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 Maneepongsawat
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry for my english...i hope u understand..
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
another notice what i sayed rely on that the synchrized methods are instance members of the calss.
 
Weerawit Maneepongsawat
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry curentThread() is a method
 
Let's go to the waterfront with this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic