aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes yield() 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 "yield()" Watch "yield()" New topic
Author

yield()

ashok khetan
Ranch Hand

Joined: Jul 29, 2001
Posts: 153
hi! When we call the yield() method in a synchronized method, then does it releases the lock?? Try the program given below. In this program as the yield() method is called at //1, the second thread gets a chance(not always, try to run it 3-4 times, the output would be different) to run and enters in the run() method. In this way the synchronized feature of run() method losts its effect. Can u explain something here?
ashok.
__________
class TestThread extends Thread {
public synchronized void run() {
System.out.println("entered in run");
System.out.println(getPriority());
yield(); //1
System.out.println("end of run");
}
public static void main(String args[]) {
Thread t = new TestThread();
t.start();
t = new TestThread();
t.start();
}
}
__________
One more thing, i forgot that if we use the class lock in synchronization then calling yield() in the synchronized block doesn't give up the lock, as follows Even on executing several times, the output shows that lock is not given up, but should we take it as a certain behaviour?)
__________
class fort extends Thread {
public void run() {
synchronized(fort.class) {
System.out.println("entered in run");
yield(); //1
System.out.println("end of run");
}
}
public static void main(String args[]) {
new fort().start();
new fort().start();
}
}
marilyn murphy
Ranch Hand

Joined: Aug 28, 2001
Posts: 84
If a new thread is waiting on the lock, it has to wait until the first thread releases the lock.

yield() does not cause the thread to give up the lock.

When the first thread exits the synchronized block (in this case the run() method), it will release the lock.

Does this answer your question?
galen wang
Greenhorn

Joined: Sep 21, 2001
Posts: 13
the same problom as your topic sleep()
the point is only those threads' synchronized method runs inside an object then the synchronize is valid.
1)
for example
code
-----
class TestThread extends Thread {
String ClassName;
TestThread(String classname) {
ClassName = classname;
}
public synchronized void run() {
while(true) {
System.out.println(ClassName);
}
}
public static void main(String args[]) {
new TestThread("class object 1").start();
new TestThread("class object 2").start();
}
}
-----
will output something like
......
class object 2
class object 1
class object 2
class object 1
class object 2
class object 2
......
that's because two thread objects are created and each has its own run() method. two objects can not be synchronized.
2)
if you modify the statement
synchronized(fort.class) { => synchronized(this) {
in your second code, then the result will be same as your first codes. that's alse because two objects can not be synchronized.
3)
code
-----
class synchronizedPart {
public synchronized void run(TestThread t) {
System.out.println("entered in run");
System.out.println(t.getPriority());
t.yield(); //1
System.out.println("end of run");
}
}
class TestThread extends Thread {
synchronizedPart sp;
TestThread(synchronizedPart sp) {
this.sp = sp;
}
public synchronized void run() {
sp.run(this);
}
public static void main(String args[]) {
synchronizedPart sp = new synchronizedPart();
Thread t = new TestThread(sp);
t.start();
t = new TestThread(sp);
t.start();
}
}
-----
will get the same result as your original second code, although two threads are created but the method
synchronizedPart.run()
belong to one object, so this method can be synchronized.
 
 
subject: yield()