1. 程式人生 > >第3章 多線程安全問題產生&解決方案

第3章 多線程安全問題產生&解決方案

Java

1.1 多線程賣票案例
需求:用三個線程模擬三個售票窗口,共同賣100張火車票,每個線程打印出賣第幾張票
1.1.1 案例代碼三:

package com.itheima_03;
public class TicketThread implements Runnable {
int tickets = 100;//火車票數量
@Override
public void run() {
//出售火車票
while(true) {
//當火車票小於0張,則停止售票
if(tickets > 0) {
/*
 * t1,t2,t3
 * 假設只剩一張票
 * t1過來了,他一看有票,他就進來了,但是他突然肚子不舒服,然後他就去上衛生間了
 * t2也過來了,他一看也有票,他也進來了,但是他的肚子也不舒服,他也去上衛生間了
 *
 * t1上完了衛生間回來了,開始售票
 * tickets = 0;
 * t2也上完衛生間回來了,他也進行售票
 *  tickets = -1;
 *
 *
 */
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ":" +tickets--);
}
}
}

}

1.2 多線程安全問題解決
1.2.1 使用同步代碼塊解決

格式:[/align] synchronized(鎖對象){
//需要同步的代碼
}
1.2.1.1 案例代碼四:

 package com.itheima_04;
/*
 * 問題出現的原因:
 * 要有多個線程
 * 要有被多個線程所共享的數據
 * 多個線程並發的訪問共享的數據
 *
 * 在火車上上廁所
 * 張三來了,一看門是綠的,他就進去了,把門鎖上了,門就變紅了
 * 李四來了,一看門市紅色的,他就只能憋著
 * 張三用完了廁所,把鎖打開了,門就變成了綠色
 * 李四一看門變綠了,他就進去了,把門鎖上,門就變紅了
 * 王五來了,一看們是紅色的,他也只能憋著
 * 李四用完測試了,把鎖打開了,肚子又不舒服了,扭頭回去了,又把門鎖上了,
 *
 * synchronized:同步(鎖),可以修飾代碼塊和方法,被修飾的代碼塊和方法一旦被某個線程訪問,則直接鎖住,其他的線程將無法訪問
 *
 * 同步代碼塊:
 * synchronized(鎖對象){
 *
 * }
 *
 * 註意:鎖對象需要被所有的線程所共享
 *
 *
 * 同步:安全性高,效率低
 * 非同步:效率高,但是安全性低
 *
 */
public class TicketThread implements Runnable {
int tickets = 100;//火車票數量
Object obj = new Object();
@Override
public void run() {
//出售火車票
while(true) {
synchronized (obj) {

if(tickets > 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ":" +tickets--);
}
}
}
}

}
package com.itheima_04;

public class TicktetTest {
public static void main(String[] args) {
//創建線程對象
TicketThread tt = new TicketThread();
Thread t = new Thread(tt);
t.setName("窗口1");
Thread t2 = new Thread(tt);
t2.setName("窗口2");
Thread t3 = new Thread(tt);
t3.setName("窗口3");
//啟動線程對象
t.start();
t2.start();
t3.start();
}
}
1.2.2 使用同步方法解決
[AppleScript] 純文本查看 復制代碼
?
1
2
3
4
格式:
  修飾符 synchronized 返回值 方法名(){

}

1.2.2.1 案例代碼五:

package com.itheima_05;
/*
 * 同步方法:使用關鍵字synchronized修飾的方法,一旦被一個線程訪問,則整個方法全部鎖住,其他線程則無法訪問
 *
 * synchronized
 * 註意:
 * 非靜態同步方法的鎖對象是this
 * 靜態的同步方法的鎖對象是當前類的字節碼對象
 */
public class TicketThread implements Runnable {
static int tickets = 100;// 火車票數量
Object obj = new Object();

@Override
public void run() {
// 出售火車票
while (true) {
/*synchronized (obj) {
method();
}*/
//method();
method2();

}
}

private synchronized void method() {
if (tickets > 0) {

try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

System.out.println(Thread.currentThread().getName() + ":" + tickets--);
}
}
private static synchronized void method2() {
if (tickets > 0) {

try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

System.out.println(Thread.currentThread().getName() + ":" + tickets--);
}
}

}
package com.itheima_05;

public class TicktetTest {
public static void main(String[] args) {
//創建線程對象
TicketThread tt = new TicketThread();
Thread t = new Thread(tt);
t.setName("窗口1");
Thread t2 = new Thread(tt);
t2.setName("窗口2");
Thread t3 = new Thread(tt);
t3.setName("窗口3");
//啟動線程對象
t.start();
t2.start();
t3.start();
}
}

第3章 多線程安全問題產生&解決方案