• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

yield()

 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}
}
 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
This is my favorite show. And this is my favorite tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic