线程同步机制:
一些敏感的数据不允许被多个线程同时访问,此时使用同步机制访问技术,保证数据在任何时刻,最多有一个线程访问,以保证数据的完整性。
实现同步的方式:
解决售票超卖的问题:
使用synchronized的方法:
public class test_synchronized {
public static void main(String[] args) {
thread03 thread03 = new thread03();
Thread thread1 = new Thread(thread03);
Thread thread2 = new Thread(thread03);
Thread thread3 = new Thread(thread03);
thread1.start();
thread2.start();
thread3.start();
}
}
class thread03 implements Runnable{
private int tickets=100;
private boolean loop=true;
//设置同步锁
public synchronized void sell() throws InterruptedException {
if (tickets <=0){
loop =false;
System.out.println("售票结束...");
return;
}
Thread.sleep(50);
//还剩余多少张票:
System.out.println("窗口售出一张票,"+"还剩余票数是:"+(--tickets) +","+ Thread.currentThread().getName());
}
@Override
public void run() {
while (loop){
try {
sell();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
窗口售出一张票,还剩余票数是:99,Thread-0
窗口售出一张票,还剩余票数是:98,Thread-2
窗口售出一张票,还剩余票数是:97,Thread-1
窗口售出一张票,还剩余票数是:96,Thread-2
窗口售出一张票,还剩余票数是:95,Thread-0
窗口售出一张票,还剩余票数是:94,Thread-2
窗口售出一张票,还剩余票数是:93,Thread-1
窗口售出一张票,还剩余票数是:92,Thread-2
窗口售出一张票,还剩余票数是:91,Thread-0
窗口售出一张票,还剩余票数是:90,Thread-2
窗口售出一张票,还剩余票数是:89,Thread-1
窗口售出一张票,还剩余票数是:88,Thread-1
窗口售出一张票,还剩余票数是:87,Thread-2
窗口售出一张票,还剩余票数是:86,Thread-2
窗口售出一张票,还剩余票数是:85,Thread-2
窗口售出一张票,还剩余票数是:84,Thread-2
窗口售出一张票,还剩余票数是:83,Thread-0
窗口售出一张票,还剩余票数是:82,Thread-2
窗口售出一张票,还剩余票数是:81,Thread-1
窗口售出一张票,还剩余票数是:80,Thread-2
窗口售出一张票,还剩余票数是:79,Thread-0
窗口售出一张票,还剩余票数是:78,Thread-2
窗口售出一张票,还剩余票数是:77,Thread-2
窗口售出一张票,还剩余票数是:76,Thread-1
窗口售出一张票,还剩余票数是:75,Thread-2
窗口售出一张票,还剩余票数是:74,Thread-2
窗口售出一张票,还剩余票数是:73,Thread-2
窗口售出一张票,还剩余票数是:72,Thread-2
窗口售出一张票,还剩余票数是:71,Thread-2
窗口售出一张票,还剩余票数是:70,Thread-2
窗口售出一张票,还剩余票数是:69,Thread-0
窗口售出一张票,还剩余票数是:68,Thread-0
窗口售出一张票,还剩余票数是:67,Thread-0
窗口售出一张票,还剩余票数是:66,Thread-2
窗口售出一张票,还剩余票数是:65,Thread-1
窗口售出一张票,还剩余票数是:64,Thread-1
窗口售出一张票,还剩余票数是:63,Thread-2
窗口售出一张票,还剩余票数是:62,Thread-0
窗口售出一张票,还剩余票数是:61,Thread-2
窗口售出一张票,还剩余票数是:60,Thread-1
窗口售出一张票,还剩余票数是:59,Thread-2
窗口售出一张票,还剩余票数是:58,Thread-0
窗口售出一张票,还剩余票数是:57,Thread-2
窗口售出一张票,还剩余票数是:56,Thread-2
窗口售出一张票,还剩余票数是:55,Thread-2
窗口售出一张票,还剩余票数是:54,Thread-2
窗口售出一张票,还剩余票数是:53,Thread-1
窗口售出一张票,还剩余票数是:52,Thread-2
窗口售出一张票,还剩余票数是:51,Thread-0
窗口售出一张票,还剩余票数是:50,Thread-2
窗口售出一张票,还剩余票数是:49,Thread-2
窗口售出一张票,还剩余票数是:48,Thread-1
窗口售出一张票,还剩余票数是:47,Thread-1
窗口售出一张票,还剩余票数是:46,Thread-2
窗口售出一张票,还剩余票数是:45,Thread-0
窗口售出一张票,还剩余票数是:44,Thread-0
窗口售出一张票,还剩余票数是:43,Thread-0
窗口售出一张票,还剩余票数是:42,Thread-0
窗口售出一张票,还剩余票数是:41,Thread-2
窗口售出一张票,还剩余票数是:40,Thread-1
窗口售出一张票,还剩余票数是:39,Thread-2
窗口售出一张票,还剩余票数是:38,Thread-0
窗口售出一张票,还剩余票数是:37,Thread-0
窗口售出一张票,还剩余票数是:36,Thread-0
窗口售出一张票,还剩余票数是:35,Thread-0
窗口售出一张票,还剩余票数是:34,Thread-2
窗口售出一张票,还剩余票数是:33,Thread-1
窗口售出一张票,还剩余票数是:32,Thread-1
窗口售出一张票,还剩余票数是:31,Thread-1
窗口售出一张票,还剩余票数是:30,Thread-2
窗口售出一张票,还剩余票数是:29,Thread-2
窗口售出一张票,还剩余票数是:28,Thread-2
窗口售出一张票,还剩余票数是:27,Thread-0
窗口售出一张票,还剩余票数是:26,Thread-0
窗口售出一张票,还剩余票数是:25,Thread-2
窗口售出一张票,还剩余票数是:24,Thread-2
窗口售出一张票,还剩余票数是:23,Thread-1
窗口售出一张票,还剩余票数是:22,Thread-1
窗口售出一张票,还剩余票数是:21,Thread-1
窗口售出一张票,还剩余票数是:20,Thread-2
窗口售出一张票,还剩余票数是:19,Thread-0
窗口售出一张票,还剩余票数是:18,Thread-0
窗口售出一张票,还剩余票数是:17,Thread-0
窗口售出一张票,还剩余票数是:16,Thread-2
窗口售出一张票,还剩余票数是:15,Thread-1
窗口售出一张票,还剩余票数是:14,Thread-2
窗口售出一张票,还剩余票数是:13,Thread-0
窗口售出一张票,还剩余票数是:12,Thread-0
窗口售出一张票,还剩余票数是:11,Thread-2
窗口售出一张票,还剩余票数是:10,Thread-2
窗口售出一张票,还剩余票数是:9,Thread-1
窗口售出一张票,还剩余票数是:8,Thread-1
窗口售出一张票,还剩余票数是:7,Thread-1
窗口售出一张票,还剩余票数是:6,Thread-2
窗口售出一张票,还剩余票数是:5,Thread-0
窗口售出一张票,还剩余票数是:4,Thread-2
窗口售出一张票,还剩余票数是:3,Thread-2
窗口售出一张票,还剩余票数是:2,Thread-1
窗口售出一张票,还剩余票数是:1,Thread-1
窗口售出一张票,还剩余票数是:0,Thread-2
售票结束...
售票结束...
互斥锁的概念:
注意:
- 同步方法如果没有使用static修饰,默认锁的对象是this,
- 如果方法使用的用static修饰,默认锁的对象是类名.class