两种方式实现,一种是通过synchronized方法实现,一种是通过synchronized代码段实现。两种实现方法的本质是一样的,占着一个资源求另一个资源,下面分别列出两种实现。
1.synchronized方法
Alphonse and Gaston are friends, and great believers in courtesy. A strict rule of courtesy is that when you bow to a friend, you must remain bowed until your friend has a chance to return the bow. Unfortunately, this rule does not account for the possibility that two friends might bow to each other at the same time. This example application, Deadlock, models this possibility:
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();
    }
}
When Deadlock runs, it's extremely likely that both threads will block when they attempt to invoke bowBack. Neither block will ever end, because each thread is waiting for the other to exit bow.
 
2.synchronized代码段
public   class  TestDeadLock  implements  Runnable  ... {
    
public int flag = 1;
    
static Object o1 = new Object(), o2 = new Object();
    
public void run() ...{
        System.out.println(
"flag=" + flag);
        
if(flag == 1...{
            
synchronized(o1) ...{
                
try ...{
                    Thread.sleep(
500);
                }
 catch (Exception e) ...{
                    e.printStackTrace();
                }

                
synchronized(o2) ...{
                    System.out.println(
"1");
                }

            }

        }

        
if(flag == 0...{
            
synchronized(o2) ...{
                
try ...{
                    Thread.sleep(
500);
                }
 catch (Exception e) ...{
                    e.printStackTrace();
                }

                
synchronized(o1) ...{
                    System.out.println(
"0");
                }

            }

        }

    }
    
    
    
public static void main(String[] args) ...{
        TestDeadLock td1 
= new TestDeadLock();
        TestDeadLock td2 
= new TestDeadLock();
        td1.flag 
= 1;
        td2.flag 
= 0;
        Thread t1 
= new Thread(td1);
        Thread t2 
= new Thread(td2);
        t1.start();
        t2.start();
        
    }

}
 

0

收藏

sbxxs

16篇文章,2W+人气,0粉丝