鐵道部發布了一個售票任務,要求銷售1000張票,要求有3個窗口來進行銷售,請編寫多線程程序來模擬這個效果。
1 線程類
測試方法:
1
2
3
4
5
6
7
8
9
10
|
public static void main(String[] args) { MyThread t1 = new MyThread( "窗口1" ); MyThread t2 = new MyThread( "窗口1" ); MyThread t3 = new MyThread( "窗口1" ); t1.start(); t2.start(); t3.start(); } |
1.1 局部加鎖
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
public class MyThread extends Thread{ private static int ticket = 1000 ; private static Object obj = new Object(); public MyThread(String name) { super (name); } @Override public void run() { while (ticket > 0 ){ synchronized (obj){ if (ticket > 0 ){ System.out.println(Thread.currentThread().getName() + "正在銷售第" +( 1001 -ticket)+ "張票" ); ticket--; } if (ticket <= 0 ){ System.out.println(Thread.currentThread().getName() + "票已售罄" ); } } } } } |
1.2 方法加鎖
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
public class MyThread extends Thread{ private static int ticket = 1000 ; public MyThread(String name) { super (name); } @Override public void run() { while (ticket > 0 ){ method02(); } } //鎖對象:類的字節碼文件對象(MyThread.class),有static修飾 public static synchronized void method02(){ if (ticket > 0 ){ System.out.println(Thread.currentThread().getName() + "正在銷售第" +( 1001 -ticket)+ "張票" ); ticket--; } if (ticket <= 0 ){ System.out.println(Thread.currentThread().getName() + "票已售完" ); } } } |
1.3 手動加鎖
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
public class MyThread extends Thread{ private static int ticket = 1000 ; private static Lock lock = new ReentrantLock(); public MyThread(String name) { super (name); } @Override public void run() { while (ticket > 0 ){ lock.lock(); //手動上鎖 if (ticket > 0 ){ System.out.println(Thread.currentThread().getName() + "正在銷售第" +( 1001 -ticket)+ "張票" ); ticket--; } if (ticket <= 0 ){ System.out.println(Thread.currentThread().getName() + "票已售完" ); } lock.unlock(); //手動解鎖 } } } |
2 任務類
測試方法:
1
2
3
4
5
6
7
8
9
10
|
public static void main(String[] args) { Task task = new Task(); Thread t1 = new Thread(task, "窗口1" ); Thread t2 = new Thread(task, "窗口2" ); Thread t3 = new Thread(task, "窗口3" ); t1.start(); t2.start(); t3.start(); } |
2.1 局部加鎖
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
public class Task implements Runnable{ private int tickets= 1000 ; @Override public void run() { while (tickets> 0 ){ synchronized ( this ) { if (tickets > 0 ) { System.out.printf ( "%s窗口正在售出第%d張票\n" ,Thread.currentThread().getName(), 1001 -tickets); tickets--; } if (tickets<= 0 ){ System.out.printf( "%s窗口售罄\n" ,Thread.currentThread().getName()); } } } } } |
2.2 方法加鎖
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public class Task implements Runnable{ private int tickets= 1000 ; @Override public void run() { while (tickets> 0 ){ method(); } } //方法加鎖,沒有使用static修飾 public synchronized void method(){ if (tickets > 0 ) { System.out.printf ( "%s窗口正在售出第%d張票\n" ,Thread.currentThread().getName(), 1001 -tickets); tickets--; } if (tickets<= 0 ){ System.out.printf( "%s窗口售罄\n" ,Thread.currentThread().getName()); } } } |
2.3 手動加鎖
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class Task implements Runnable{ private int tickets= 1000 ; private Lock lock = new ReentrantLock(); @Override public void run() { while (tickets> 0 ){ lock.lock(); //手動上鎖 if (tickets > 0 ) { System.out.printf ( "%s窗口正在售出第%d張票\n" ,Thread.currentThread().getName(), 1001 -tickets); tickets--; } if (tickets<= 0 ){ System.out.printf( "%s窗口售罄\n" ,Thread.currentThread().getName()); } lock.unlock(); //手動關鎖 } } } |
效果截圖:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/DengShuo_shuai/article/details/119712285