java线程同步Demo2

public class Producer extends Thread
{
    private Bread bread;
    public Producer(Bread bread)
    {
        this.bread = bread;
        this.start();
    }
    public void run()
    {
        for (int i = 0; i < 5; i++)
        {
            bread.push(); // 执行完后即释放对象锁
            try
            {
                sleep(500); // 休眠让其他线程有机会执行
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }
    }
}

你可能感兴趣的:(java)