/**
* 多线程的含义是:多个线程操作的是同一个对象
*/
public class MyThread003 implements Runnable
{
private static final String TAG = "MyThread003";
private int apple = 100;
synchronized public void sell()//方法锁
{
System.out.println(Thread.currentThread().getName() + "卖出了第" + " " + apple + " " + "apple");
--apple;
}
public int getApple()
{
return apple;
}
@Override
public void run()
{
while (getApple() > 0)
{
sell();
try
{
Thread.sleep(200);
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
System.out.println("卖光了!");
}
public static void main(String[] args)
{
MyThread003 m3 = new MyThread003();
Thread t1 = new Thread(m3, "zhanlo");
Thread t2 = new Thread(m3, "lisi");
Thread t3 = new Thread(m3, "jhon");
Thread t4 = new Thread(m3, "puple");
t1.start();
t2.start();
t3.start();
t4.start();
}
}