线程同步
多个线程操作同一个资源 并发:同一个对象被多个线程同时操作
1.线程同步
处理多线程问题时,多个线程访问同一个对象,并且某些线程还想修改这个对象,这个时候我们就需要线程同步,线程同步其实就是一种等待机制,多个需要同时访问此对象的线程进入这个对象的等待池 ,形成队列,等待前面线程使用完毕,下一个线程再使用 由于同一进程的多个线程共享同一块存储空间,再带来便利的同时,也带来了访问冲突问题,为了保证数据再方法中被访问时候的正确性,在访问时加入锁机制synchronized ,当一个线程获得对象的排它锁,独占资源,其他线程必须等待,使用后释放锁即可
锁存在问题
一个线程持有锁会导致其他所有需要此锁的线程挂起 在多线程竞争下,加锁,释放锁会导致比较多的上下文切换和调度延时,引起性能问题; 如果一个优先级高的线程等待一个优先级低的线程释放锁,会导致优先级倒置,引起性能问题
2.线程同步安全问题示例
a.多线程操作同一个资源(购票)
package thread. synchronize;
public class UnsafeBuyTicket {
public static void main ( String[ ] args) {
BuyTicket buyTicket = new BuyTicket ( ) ;
new Thread ( buyTicket, "Thread01" ) . start ( ) ;
new Thread ( buyTicket, "Thread02" ) . start ( ) ;
new Thread ( buyTicket, "Thread03" ) . start ( ) ;
}
}
class BuyTicket implements Runnable {
private int ticketsNum = 10 ;
boolean flag = true ;
@Override
public void run ( ) {
while ( flag) {
try {
buy ( ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
}
private void buy ( ) throws InterruptedException {
if ( ticketsNum <= 0 ) {
flag = false ;
return ;
}
Thread. sleep ( 100 ) ;
System. out. println ( Thread. currentThread ( ) . getName ( ) + "get the ticket" + ticketsNum-- ) ;
}
}
b.多线程操作同一个资源(取款)
package thread. synchronize;
public class UnsafeBank {
public static void main ( String[ ] args) {
Account accout = new Account ( 100 , "WeddingMoney" ) ;
Drawing michael = new Drawing ( accout, 50 , "MICHAEL" ) ;
Drawing nathan = new Drawing ( accout, 90 , "NATHAN" ) ;
michael. start ( ) ;
nathan. start ( ) ;
}
}
class Account {
int money;
String name;
public Account ( int money, String name) {
this . money = money;
this . name = name;
}
}
class Drawing extends Thread {
Account account;
int drawingMoney;
int nowMoney;
public Drawing ( Account account, int drawingMoney, String name) {
super ( name) ;
this . account = account;
this . drawingMoney = drawingMoney;
}
@Override
public void run ( ) {
if ( account. money - drawingMoney < 0 ) {
System. out. println ( Thread. currentThread ( ) . getName ( ) + "current money is not enough" ) ;
return ;
}
try {
Thread. sleep ( 1000 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
account. money = account. money - drawingMoney;
nowMoney = nowMoney + drawingMoney;
System. out. println ( account. name+ "account current money is :" + account. money) ;
System. out. println ( this . getName ( ) + "account current nowMoney is :" + nowMoney) ;
}
}
c.多线程操作同一个资源(list操作)
package thread. synchronize;
import java. util. ArrayList;
public class UnsafeList {
public static void main ( String[ ] args) {
ArrayList< String> list = new ArrayList < > ( ) ;
for ( int i = 0 ; i < 10000 ; i++ ) {
new Thread ( ( ) - > {
list. add ( Thread. currentThread ( ) . getName ( ) ) ;
} ) . start ( ) ;
}
System. out. println ( list. size ( ) ) ;
}
}
2.线程同步方法
由于我们可以通过private关键字来保证数据对象只能被方法访问,所有我们只需要针对方法提出一套机制,这套机制就是synchronized关键字,它包括两种方法:synchronized 方法 和synchronized 块
public synchronized void method ( int args) { }
synchronized方法控制对“对象”的访问,每个对象一把锁,每个synchronized方法都必须获得调用该方法的对象的锁才能执行,否则线程会阻塞,方法一旦执行,就独占该锁,直到方法返回才释放锁后面被阻塞的线程才能获得这个锁,继续执行; 缺陷: 若将一个大的方法申明为synchronized将会影响效率
a.同步方法(解决多线程安全问题示例一)
package thread. synchronize;
public class UnsafeBuyTicket {
public static void main ( String[ ] args) {
BuyTicket buyTicket = new BuyTicket ( ) ;
new Thread ( buyTicket, "Thread01" ) . start ( ) ;
new Thread ( buyTicket, "Thread02" ) . start ( ) ;
new Thread ( buyTicket, "Thread03" ) . start ( ) ;
}
}
class BuyTicket implements Runnable {
private int ticketsNum = 10 ;
boolean flag = true ;
@Override
public void run ( ) {
while ( flag) {
try {
buy ( ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
}
private synchronized void buy ( ) throws InterruptedException {
if ( ticketsNum <= 0 ) {
flag = false ;
return ;
}
Thread. sleep ( 100 ) ;
System. out. println ( Thread. currentThread ( ) . getName ( ) + "get the ticket" + ticketsNum-- ) ;
}
}
b.同步块(解决多线程安全问题示例二三)
(1)同步块:synchronized(Obj){}
(2)Obj称之为同步监视器
Obj可以是任何对象,但是推荐使用共享资源作为同步监视器 同步方法中无需指定同步监视器,因为同步方法的同步监视器就是this,就是这个对象本身,或者class
(3)同步监视器的执行过程
第一个线程访问,锁定同步监视器,执行其中代码 第二个线程访问,发现同步监视器被锁定,无法访问 第一个线程访问完毕,解锁同步监视器 第二个线程访问,发现同步监视器没有锁,然后锁定并访问
package thread. synchronize;
public class UnsafeBank {
public static void main ( String[ ] args) {
Account accout = new Account ( 100 , "WeddingMoney" ) ;
Drawing michael = new Drawing ( accout, 50 , "MICHAEL" ) ;
Drawing nathan = new Drawing ( accout, 90 , "NATHAN" ) ;
michael. start ( ) ;
nathan. start ( ) ;
}
}
class Account {
int money;
String name;
public Account ( int money, String name) {
this . money = money;
this . name = name;
}
}
class Drawing extends Thread {
Account account;
int drawingMoney;
int nowMoney;
public Drawing ( Account account, int drawingMoney, String name) {
super ( name) ;
this . account = account;
this . drawingMoney = drawingMoney;
}
@Override
public void run ( ) {
synchronized ( account) {
if ( account. money - drawingMoney < 0 ) {
System. out. println ( Thread. currentThread ( ) . getName ( ) + "current money is not enough" ) ;
return ;
}
try {
Thread. sleep ( 1000 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
account. money = account. money - drawingMoney;
nowMoney = nowMoney + drawingMoney;
System. out. println ( account. name+ "account current money is :" + account. money) ;
System. out. println ( this . getName ( ) + "account current nowMoney is :" + nowMoney) ;
}
}
}
package thread. synchronize;
import java. util. ArrayList;
public class UnsafeList {
public static void main ( String[ ] args) {
ArrayList< String> list = new ArrayList < > ( ) ;
for ( int i = 0 ; i < 10000 ; i++ ) {
new Thread ( ( ) - > {
synchronized ( list) {
list. add ( Thread. currentThread ( ) . getName ( ) ) ;
}
} ) . start ( ) ;
}
try {
Thread. sleep ( 1000 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
System. out. println ( list. size ( ) ) ;
}
}