日期:2014-05-20 浏览次数:20715 次
package cn.dzr.other;
abstract class Animal
{
int i=11;
Object o = new Object();
public void beforeEat()
{
System.out.println("Animal ready to eat!");
}
public void eat() throws InterruptedException
{
synchronized(o)
{
System.out.println(i);
Thread.sleep(3000);
beforeEat();
System.out.println("Animal eat!");
}
}
}
class Tiger extends Animal implements Runnable
{
public void beforeEat()
{
System.out.println("Tiger ready to eat!");
}
int i = 13;
public void run()
{
try
{
eat();
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class Fish extends Animal implements Runnable
{
public void beforeEat()
{
System.out.println("Fish ready to eat!");
}
public void run()
{
try
{
eat();
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
int i = 14;
}
class testExtend implements Runnable
{
static Object obj = new Object();
public void run()
{
synchronized(obj)
{
System.out.println(Thread.currentThread().getName()+"sleep前....");
try
{
Thread.sleep(3000);
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("3");
System.out.println(Thread.currentThread().getName()+"sleep后....");
}
}
}
public class InterfaceTest
{
public static void main(String[] args) throws InterruptedException
{
Fish f = new Fish();
Tiger t = new Tiger();
new Thread(f).start();
new Thread(t).start();
new Thread(new testExtend()).start();