synchronize
public void synchronize(Line[] lines,
                        boolean maintainSync)
Synchronizes two or more lines. Any subsequent command that starts or stops audio playback or capture for one of these lines will exert the same effect on the other lines in the group, so that they start or stop playing or capturing data simultaneously. Parameters:
lines - the lines that should be synchronized
maintainSync - true if the synchronization must be precisely maintained (i.e., the synchronization must be sample-accurate) at all times during operation of the lines , or false if precise synchronization is required only during start and stop operations 
Throws: 
IllegalArgumentException - if the lines cannot be synchronized. This may occur if the lines are of different types or have different formats for which this mixer does not support synchronization, or if all lines specified do not belong to this mixer.

解决方案 »

  1.   

    public class Foo
    {
        public synchronized void f1()
        {
        }    public void f2()
        {    }
    }
      

  2.   

    刚才是误操作
    public class Foo
    {
        public synchronized void f1()
        {
        }    public void f2()
        {
            synhronized(this)
            {
            }
        }
    }
    以上两个方法的synhronized关键字的作用是一样的,都是用于取得和释放this的对象锁
    但是f1生成的字节码会小一点
    因为对于方法一,生成的字节码和不加synhronized关键字的方法public void f1(){}是一样的,唯一的区别在于f1的一个SYNC属性会为设置为真,
    对于方法f2,会在里面生成相应的字节码,用于取得和释放this的对象锁
      

  3.   

    在《JAVA技术实例手册》里第四章练习4-3中是这么写的:
    例4-3用synchronized语句演示了死锁。写一个类似的程序,但是使用synchronized方法而不是用synchronized语句。这种类型的死锁有一点微秒,且难于检测出来。无需多说,从效果来讲,两者是相同的,但是为什么synchronized语句会难于检测?