class Sou implements Runnable {
static boolean falg = true;
private static int a = 100; //实例变量
int count = 1;// 定义一个记数器,看最终是不是100张票.
public synchronized void sell() {
int a = 200;//局部变量
while (a > 0) {
if (a > 0) {
System.out.println(Thread.currentThread().getName() + ": " + a
+ "sell--top---" + count++);
try {
Thread.sleep(10);
} catch (Exception e) {
}
a--;
// System.out.println(Thread.currentThread().getName() + ": " + a
// + "sell---end--" + count++);
}
}
}
public void run() {
int a = 150;//局部变量
if (falg) {
while (a > 0) {
synchronized (Sou.class) { //锁标记 this
if (a > 0) {
System.out.println(Thread.currentThread().getName()
+ ": " + a + "run-----" + count++);
try {
Thread.sleep(10);
} catch (Exception e) {
}
a--;
}