同步方法及代码块:synchronized
同步方法
由于我们可以通过private关键字来保证数据对象只能被方法访问,所以我们只需要针对方法提出一套机制,这套机制就是synchronized关键字,它包括两种用法:synchronized方法和sychronized块
同步方法:public synchronized void method ( int args) { }
synchronized方法控制对“对象”的访问,每个对象对应一把锁,每个synchronized方法都必须获得调用该方法的对象的锁才能执行,否则线程会阻塞,方法一旦执行,就独占该锁,直到该方法返回才释放锁,后面被阻塞的线程才能获得这个锁,继续执行。
缺陷:若将一个打的方法申明为 synchronized 将会影响效率
同步块
同步块:synchronized(Obj){} Obj:称之为同步监视器
Obj可以是任何对象,但是推荐使用共享资源作为同步监视器; 同步方法中无需指定同步监视器,因为同步方法的同步监视器就是this,就是这个对象本身,或者class。 同步监视器的执行过程
第一个线程访问,锁定同步监视器,执行其中代码; 第二个线程访问,发现同步监视器被锁定,无法访问; 第一个线程访问完毕,解锁同步监视器; 第二个线程访问,发现同步监视器没有锁,然后锁定并访问。
不安全的买票
public class Ticked {
public static void main ( String [ ] args) {
Buy buy = new Buy ( ) ;
new Thread ( buy, "云疏" ) . start ( ) ;
new Thread ( buy, "若瑄" ) . start ( ) ;
new Thread ( buy, "淼淼" ) . start ( ) ;
}
}
public class Buy implements Runnable {
private int ticketNums = 10 ;
boolean flag = true ;
@Override
public void run ( ) {
while ( flag) {
try {
Thread . sleep ( 1000 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
buyTicket ( ) ;
}
}
public void buyTicket ( ) {
if ( ticketNums <= 0 ) {
flag = false ;
return ;
}
System . out. println ( Thread . currentThread ( ) . getName ( ) + "抢到了第" + ticketNums-- + "票" ) ;
}
}
public synchronized void buyTicket ( ) {
if ( ticketNums <= 0 ) {
flag = false ;
return ;
}
System . out. println ( Thread . currentThread ( ) . getName ( ) + "抢到了第" + ticketNums-- + "票" ) ;
}
}
不安全的银行取钱
public class Money {
public static void main ( String [ ] args) {
Account account = new Account ( 100 , "结婚基金" ) ;
Bank you = new Bank ( account, 50 , "你" ) ;
Bank girlFriend = new Bank ( account, 100 , "女朋友" ) ;
new Thread ( you, "你" ) . start ( ) ;
new Thread ( girlFriend, "女朋友" ) . start ( ) ;
}
}
public class Account {
int money;
String name;
public Account ( int money, String name) {
this . money = money;
this . name = name;
}
}
public class Bank implements Runnable {
Account account;
int getMoney;
int nowMoney;
String name;
public Bank ( Account account, int getMoney, String name) {
this . account = account;
this . getMoney = getMoney;
this . name = name;
}
@Override
public void run ( ) {
if ( account. money - getMoney < 0 ) {
System . out. println ( Thread . currentThread ( ) . getName ( ) + "取不了,钱不够" ) ;
return ;
}
try {
Thread . sleep ( 3000 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
account. money = account. money- getMoney;
nowMoney = nowMoney + getMoney;
System . out. println ( account. name+ "余额为:" + account. money) ;
System . out. println ( Thread . currentThread ( ) . getName ( ) + "手里的钱为:" + nowMoney) ;
}
}
public class Bank implements Runnable {
Account account;
int getMoney;
int nowMoney;
String name;
public Bank ( Account account, int getMoney, String name) {
this . account = account;
this . getMoney = getMoney;
this . name = name;
}
@Override
public void run ( ) {
synchronized ( account) {
if ( account. money - getMoney < 0 ) {
System . out. println ( Thread . currentThread ( ) . getName ( ) + "取不了,钱不够" ) ;
return ;
}
try {
Thread . sleep ( 3000 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
account. money = account. money- getMoney;
nowMoney = nowMoney + getMoney;
System . out. println ( account. name+ "余额为:" + account. money) ;
System . out. println ( Thread . currentThread ( ) . getName ( ) + "手里的钱为:" + nowMoney) ;
}
}
}
不安全的List集合
public class ListDemo {
public static void main ( String [ ] args) {
List < String > list = new ArrayList < > ( ) ;
for ( int i = 0 ; i < 10000 ; i++ ) {
new Thread ( ( ) -> {
list. add ( Thread . currentThread ( ) . getName ( ) ) ;
} ) . start ( ) ;
}
try {
Thread . sleep ( 3000 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
System . out. println ( list. size ( ) ) ;
}
}
public class ListDemo {
public static void main ( String [ ] args) {
List < String > list = new ArrayList < > ( ) ;
for ( int i = 0 ; i < 10000 ; i++ ) {
new Thread ( ( ) -> {
synchronized ( list) {
list. add ( Thread . currentThread ( ) . getName ( ) ) ;
}
} ) . start ( ) ;
}
try {
Thread . sleep ( 3000 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
System . out. println ( list. size ( ) ) ;
}
}