多线程-同步代码块
//copyright©liupengcheng
//http://www.cnblogs.com/liupengcheng
/**
* Created by Administrator on 2014/10/24.
* synchronized 锁旗标,改变标志位,已达到同一时刻只有一个线程在执行有效操作。
*/
//http://www.cnblogs.com/liupengcheng
public class TheadDemo5 {
public static void main(String[] args)
{
ThreadTest t = new ThreadTest();
Thread t1 = new Thread(t);t1.start();
Thread t2 = new Thread(t);t2.start();
Thread t3 = new Thread(t);t3.start();
}
}
//http://www.cnblogs.com/liupengcheng
class ThreadTest implements Runnable {
private int tickets = 100;
String str = new String("");
public void run() {
while (true) {
synchronized (str) {
if (tickets > 0) {
try {
Thread.sleep(10);
} catch (Exception e) {
System.out.println(e.getMessage());
}
System.out.println(Thread.currentThread().getName() + "is saling ticket" + tickets--);
}
}
}
}
}
//http://www.cnblogs.com/liupengcheng
/**
* 结果为
* Thread-0is saling ticket100
Thread-0is saling ticket99
Thread-0is saling ticket98
Thread-0is saling ticket97
Thread-0is saling ticket96
Thread-0is saling ticket95
Thread-2is saling ticket94
Thread-2is saling ticket93
Thread-2is saling ticket92
Thread-2is saling ticket91
Thread-2is saling ticket90
Thread-2is saling ticket89
Thread-2is saling ticket88
Thread-2is saling ticket87
Thread-2is saling ticket86
Thread-2is saling ticket85
Thread-2is saling ticket84
Thread-2is saling ticket83
Thread-2is saling ticket82
Thread-2is saling ticket81
Thread-2is saling ticket80
Thread-2is saling ticket79
Thread-2is saling ticket78
Thread-2is saling ticket77
Thread-2is saling ticket76
Thread-2is saling ticket75
Thread-2is saling ticket74
Thread-2is saling ticket73
Thread-2is saling ticket72
Thread-2is saling ticket71
Thread-2is saling ticket70
Thread-2is saling ticket69
Thread-2is saling ticket68
Thread-2is saling ticket67
Thread-2is saling ticket66
Thread-2is saling ticket65
Thread-2is saling ticket64
Thread-2is saling ticket63
Thread-2is saling ticket62
Thread-2is saling ticket61
Thread-2is saling ticket60
Thread-2is saling ticket59
Thread-2is saling ticket58
Thread-2is saling ticket57
Thread-2is saling ticket56
Thread-2is saling ticket55
Thread-2is saling ticket54
Thread-2is saling ticket53
Thread-2is saling ticket52
Thread-2is saling ticket51
Thread-2is saling ticket50
Thread-2is saling ticket49
Thread-2is saling ticket48
Thread-2is saling ticket47
Thread-2is saling ticket46
Thread-2is saling ticket45
Thread-2is saling ticket44
Thread-2is saling ticket43
Thread-2is saling ticket42
Thread-2is saling ticket41
Thread-2is saling ticket40
Thread-2is saling ticket39
Thread-2is saling ticket38
Thread-2is saling ticket37
Thread-2is saling ticket36
Thread-2is saling ticket35
Thread-2is saling ticket34
Thread-2is saling ticket33
Thread-2is saling ticket32
Thread-2is saling ticket31
Thread-2is saling ticket30
Thread-2is saling ticket29
Thread-2is saling ticket28
Thread-2is saling ticket27
Thread-2is saling ticket26
Thread-2is saling ticket25
Thread-2is saling ticket24
Thread-2is saling ticket23
Thread-2is saling ticket22
Thread-2is saling ticket21
Thread-2is saling ticket20
Thread-2is saling ticket19
Thread-2is saling ticket18
Thread-2is saling ticket17
Thread-2is saling ticket16
Thread-2is saling ticket15
Thread-2is saling ticket14
Thread-2is saling ticket13
Thread-2is saling ticket12
Thread-2is saling ticket11
Thread-2is saling ticket10
Thread-2is saling ticket9
Thread-2is saling ticket8
Thread-2is saling ticket7
Thread-2is saling ticket6
Thread-2is saling ticket5
Thread-2is saling ticket4
Thread-2is saling ticket3
Thread-2is saling ticket2
Thread-2is saling ticket1
*/
//http://www.cnblogs.com/liupengcheng