线程同步机制
1.在多线程编程,一些敏感数据不允许被多个线程同时访问,此时就使用线程同步访问技术,保证数据在任何同一时刻,最多有一个线程访问,以保证数据的完整性
2.也可以这样理解:线程同步,即当有一个线程在对内存进行操作时,其他线程都不可以对这个内存地址进行操作,直到该线程完成操作,其他线程才能对该内存地址进行操作
同步具体方法-Synchronized
1.同步代码块
synchronized(对象){ 得到对象的所,才能操作同步代码
需要被同步的代码
}
2.synchronized还可以放在方法声明中,表示整个方法-为同步方法
public synchronized void m(String name){
需要被同步的代码
}
解决了多卖问题
public class ticket {
public static void main(String[] args) {
A a1 = new A();
A a2 = new A();
A a3 = new A();
a1.start();
a2.start();
a3.start();
}
}
class A extends Thread{
private static int left = 100;
boolean loop= true;
//同步方法(静态的)的所为当前类本身
//1.public synchronized static void m1(){}锁是加在A.class本身
public synchronized static void m1(){
}
//2.如果在静态方法中,实现一个同步代码块,不能用this 需使用 类.class
public static void m2(){
synchronized(A.class){
System.out.println("m2");
}
}
//1.public synchronized void m(){}就是一个同步方法
//2.这时锁在this对现象
public synchronized void m(){//同步方法
if(left <= 0){
System.out.println("售票结束...");
loop = false;
return;
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("窗口"+ Thread.currentThread().getName() +" 剩余 " + left-- + "张票" );
}
//3.也可以在代码块加锁,同步代码块
public /*synchronized*/ void m(){
synchronized (this) {
if (left <= 0) {
System.out.println("售票结束...");
loop = false;
return;
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("窗口" + Thread.currentThread().getName() + " 剩余 " + --left + "张票");
}
}
@Override
public void run() {
while(loop){
m();
}
}
}
注意事项
1.同步方法如果没有使用static修饰:默认锁的对象为this
2.如果方法使用static修饰,默认锁对象:当前类.class