I have to questions:
1. What part of the code of a synchronized method, the synchronization block?
for example:
public class example{
public synchronized void f1(){
//some code....
f2();
}
public synchronized void f2()
{
//some code...
}
}
public void main(String[[] args)
{
Thread t1 = new Thread(new Runnable()
{public void run(){f1();)},
t2 = new Thread(new Runnable()
{public void run(){f2();};
t1.start();
t2.start();
}
so after t1 is started, t2 can't start - because its waiting for t1. But whan t1 starts doing f2, does that mean that t2 can enter f1?
And if you may, please explain this deadlock example. i didn't get it. source: http://docs.oracle.com/javase/tutorial/essential/concurrency/deadlock.html
public class Deadlock {
static class Friend {
private final String name;
public Friend(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public synchronized void bow(Friend bower) {
System.out.format("%s: %s"
+ " has bowed to me!%n",
this.name, bower.getName());
bower.bowBack(this);
}
public synchronized void bowBack(Friend bower) {
System.out.format("%s: %s"
+ " has bowed back to me!%n",
this.name, bower.getName());
}
}
public static void main(String[] args) {
final Friend alphonse = new Friend("Alphonse");
final Friend gaston = new Friend("Gaston");
new Thread(new Runnable() {
public void run() { alphonse.bow(gaston); }}).start();
new Thread(new Runnable() {
public void run() { gaston.bow(alphonse); }}).start();
}
}