一、
首先我們先大致了解一下什么是多線程。(書上的解釋)
程序是一段靜態的代碼,它是應用軟件的藍本。進程是程序的一次動態執行過程,對應了從代碼加載執行,執行到執行完畢的一個完整的過程。
線程不是進程,線程是比進程更小的執行單位,一個進程在其執行過程中,可以產生多個線程形成多條執行線索,每條線索即每個線程也有它自身的產生,存在,消亡的過程,和進程共享操作系統的資源類似,線程間也可以共享進程中的某些內存單元,并利用這些共享單元來實現數據交換,實時通信與必要的同步操作,但與進程不同的是線程的中斷和恢復更加節省開支。線程是運行在進程中的“小進程”。
多線程是指一個應用程序中同時存在幾個執行體,按幾條不同的執行線索共同工作的情況。雖然看似是幾個事件同時發生,但其實計算機在任何給定時刻只能執行那些線程中的一個。為了建立這些線程在同步進行的感覺,java虛擬機快速的把控制從一個線程切換到另一個線程。這些線程將被輪流執行,使得每個線程都有機會使用cpu資源。
二、
利用單線程實現的簡易微信發紅包
共寫有三種方法,其中第一種,第二種未設置范圍,紅包數和人數為一一對應,第三種增添了取值范圍以及計數器,人多紅包少有未搶到現象發生。
(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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
import java.util.scanner; import com.sun.deploy.security.selectablesecuritymanager; import java.util.random; public class 簡易微信發紅包 { public static void main(string[] args) { scanner scanner= new scanner(system.in); int n; double money; system.out.println( "請輸入您想要發的紅包數量" ); n=scanner.nextint(); system.out.println( "請輸入您發送的紅包金額" ); money=scanner.nextdouble(); t2 t2= new t2(n,money); t2.rob(); } } class t2 { public double remain; //有紅包被領取后的余額 int n; //紅包數量 t2( int n, double money) { this .remain=money; this .n=n; } int a= 1 ; public void rob() { while (n > 0 ) { double x2; if (n != 1 ) { //因為最后一個人領取金額為前面人領取紅包后剩下的,所以無需再進行隨機 x2 = process(); //取隨機金額 while (judge(x2) != 1 ) { //判斷取到的隨機金額是否非法,即無法保證后來每個紅包領取者領到最低金額0.01 x2 = process(); //若非法則重新取隨機金額 } remain = remain - x2; //當領取成功后余額減去領走的金額 n--; //確保每次判斷人數為所剩紅包數減1 system.out.println( "紅包獲得者" + a + "獲得" + x2 + "元" ); //此處默認領取者順序為升序 a++; //控制輸出順序 } else { x2 = remain; //因為最后一個人領取金額為前面人領取紅包后剩下的,所以無需再進行隨機 string str = string.valueof(x2); string str1 = string.format( "%.2f" , x2); x2 = double .parsedouble(str1); system.out.println( "紅包獲得者" + a + "獲得" + x2 + "元" ); n--; //確保每次判斷人數為所剩紅包數減1 } } } public int judge( double x){ //判斷函數 if (remain-x>(n- 1 )* 0.01 ){ //確保后來紅包領取者最少能領到最低金額0.01 return 1 ; } else return 0 ; } public double process() { //實現紅包金額隨機的函數 double x2; double x1; string str1; random random = new random(); //隨機數為取0到1之間的任意double值 x1 = remain*random.nextdouble(); str1= string.format( "%.2f" ,x1); //轉化成字符串型用字符串型的format進行格式化處理,紅包金額最多取到小數點后兩位 x2= double .parsedouble(str1); //再將字符串型數據轉換成double型 while (x2== 0 ){ //如果所取金額非法則回爐重造 x1 = remain*random.nextdouble(); str1= string.format( "%.2f" ,x1); //轉化成字符串型用字符串型的format進行格式化處理,紅包金額最多取到小數點后兩位 x2= double .parsedouble(str1); //再將字符串型數據轉換成double型 } return x2; } } |
程序運行結果如下
(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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
import java.util.random; import java.util.scanner; public class 簡易微信發紅包 2 { public static void main(string[] args) { scanner scanner= new scanner(system.in); double money= 0 ; //紅包總金額 int n; //紅包個數 system.out.println( "請輸入您想要發的紅包數量" ); n=scanner.nextint(); system.out.println( "請輸入您發送的紅包金額" ); money=scanner.nextdouble(); if (money/n== 0.01 ){ //當所發金額剛好為每人0.01元時 t6 t6= new t6(money,n); t6.rob(); } else { t5 t5= new t5(money,n); t5.rob(); } } } class t5{ double remain; int n; t5( double money, int n){ this .remain=money; this .n=n; } int a= 1 ; public void rob(){ double max; //最大可領紅包金額 double x1; //隨機金額 double x2; //所得金額 while (n> 0 ) { if (n != 1 ) { //前n-1個紅包領取者領的紅包為隨機金額紅包 max = remain - (n - 1 ) * 0.01 ; //最大可領紅包金額為剩下的人都獲得最小金額0.01 random random = new random(); x1 = ( double ) random.nextint(( int ) ((max - 0.01 ) * 100 )); //用nextint而不用nextdouble的原因是nextdouble無法設置seed //上式中max-0.01,下面的x2+0.01即解決了隨機數取0導致紅包獲得者沒搶到錢的問題 x1 /= 100.0 ; x2 = x1 + 0.01 ; remain = remain - x2; n--; system.out.println( "紅包獲得者" + a + "獲取金額為:" + string.format( "%.2f" , x2) + "元" ); a++; } else { //最后一人領的紅包為前n-1個人領完后剩下的紅包 system.out.println( "紅包獲得者" + a + "獲取金額為:" + string.format( "%.2f" , remain) + "元" ); n--; } } } } class t6 { double remain; int n; t6( double money, int n){ this .remain=money; this .n=n; } public void rob(){ for ( int i= 1 ;i<=n;i++){ system.out.println( "紅包獲得者" +i+ "獲得了0.01元" ); } } } |
程序運行結果如下:
(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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
import java.util.random; import java.util.scanner; public class 簡易微信發紅包 3 { public static void main(string[] args) { int p,n; double money; system.out.println( "請輸入您發送的紅包金額" ); scanner scanner= new scanner(system.in); money=scanner.nextdouble(); system.out.println( "請輸入您發送的紅包數量" ); n=scanner.nextint(); system.out.println( "請輸入參與搶紅包的人數" ); p=scanner.nextint(); t7 t7= new t7(money,n,p); t7.rob(); } } class t7 { double money; int n,p; int count = 0 ; //計數器 double remain; t7( double money, int n, int p){ this .money=money; //總金額 this .n=n; //紅包數 this .p=p; //搶紅包人數 this .remain=money; //所剩金額 } public void rob() { for ( int i= 1 ;i<=p;i++) { double x1, x2, d; string s1, s2; random random = new random(); d = money / (n - 1 ); //設置范圍讓每次所得金額不超過總數的1/(n-1),這樣也就避免了一次取得過大導致后面搶的紅包不能保證每個最少0.01 x1 = d * random.nextdouble(); s1 = string.format( "%.2f" , x1); //轉化成字符串型用字符串型的format進行格式化處理,紅包金額最多取到小數點后兩位 x1 = double .parsedouble(s1); //再將字符串型數據轉換成double型 while (x1 == 0 || x1 == money / (n - 1 )) { x1 = d * random.nextdouble(); s1 = string.format( "%.2f" , x1); //轉化成字符串型用字符串型的format進行格式化處理,紅包金額最多取到小數點后兩位 x1 = double .parsedouble(s1); //再將字符串型數據轉換成double型 } s2 = string.format( "%.2f" , remain); //轉化成字符串型用字符串型的format進行格式化處理,紅包金額最多取到小數點后兩位 remain = double .parsedouble(s2); //再將字符串型數據轉換成double型 if (count < n - 1 ) { //前n-1個紅包金額為隨機金額 system.out.println( "紅包搶奪者" +i+ "搶到了" + s1 + "元" ); remain -= x1; count++; } else if (count == n - 1 ) { //第n個為前n-1個紅包搶完所剩金額 system.out.println( "紅包搶奪者" +i+ "搶到了" + s2 + "元" ); count++; } else if (count > n - 1 ) { //紅包被搶完后再來的 system.out.println( "紅包搶奪者" +i+ "哎呀,手慢了!沒搶到!" ); count++; } } } } |
程序運行結果如下:
三、
利用多線程實現的簡易微信發紅包
那么如何創建多線程呢?
1.通過繼承thread類創建多線程
jdk中提供了一個線程類thread,通過繼承thread類,并重寫thread類中的run()方法便可實現多線程。
在thread類中,提供了一個start()方法用于啟動新線程,線程啟動后,系統會自動調用run()方法,如果子類重寫了該方法便會執行子類中的方法。
run()方法中就是寫能夠被線程執行的程序。如果直接調用則相當于普通方法,必須使用start()方法,才能啟動線程,然后再由jvm去調用該線程的run()方法。
創建并啟動多線程的步驟
①定義thread類的子類,并重寫該類的run方法,其方法體代表線程需要完成的任務。因此常把run方法稱為線程執行體。
②創建thread子類的實例,即創建線程對象。
③用線程對象的start方法來啟動該線程。
2.通過實現runnable接口創建多線程
通過繼承thread類實現了多線程,但是這種方式有一定的局限性。因為java中只支持單繼承,一個類一旦繼承了某個父類就無法再繼承thread類。
thread類提供了另外一個構造方法thread(runnable target),其中runnable是一個接口,它只有一個run()方法。
當通過thread(runnable target))構造方法創建線程對象時,只需為該方法傳遞一個實現了runnable接口的實例對象,這樣創建的線程將調用實現了runnable接口中的run()方法作為運行代碼,而不需要調用thread類中的run()方法。
創建并啟動多線程的步驟
①定義runnable接口的實現類,并重寫該接口的run方法,該run方法的方法體同樣是該線程的線程執行體。
②創建runnable實現類的實例,并以此為實例作為thread的參數來創建thread對象,該thread對象才是真正的線程對象。
當多個線程使用同一個共享資源時,可以將處理共享資源的代碼放置在一個代碼塊中,使用synchronized關鍵字來修飾,被稱作同步代碼塊:
sychronized(lock){
操作共享資源代碼塊
}
其中:lock是一個鎖對象,它是同步代碼塊的關鍵。當線程執行同步代碼塊時,首先會檢查鎖對象的標志位,默認情況下,標志位為1,此時線程會執行同步代碼塊,同時將鎖對象的標志位置為0。當一個新的線程執行到這段同步代碼塊時,由于鎖對象的標志位為0,新線程會發生阻塞,等待當前線程執行完同步代碼塊后,鎖對象的標志位被置為1,新線程才能進入同步代碼塊執行其中的代碼。循環往復,直到共享資源被處理完為止。
同步代碼塊可以有效解決線程的安全問題,當把共享資源的操作放在synchronized定義的區域內時,便為這些操作加了同步鎖。
在方法前面同樣可以使用synchronized關鍵字來修飾,被修飾的方法為同步方法,它能實現和同步代碼塊同樣的功能,具體語法格式如下:
synchronized 返回值類型 方法名 {}
被synchronized修飾的方法在某一時刻只允許一個線程訪問,訪問該方法的其它線程都會發生阻塞,直到當前線程訪問完畢后,其它線程才有機會執行方法。
另外
public final string getname():獲取線程的名稱。
public static thread currentthread():返回當前正在執行的線程對象,這樣就可以獲取任意方法所在的線程名稱。
thread.currentthread().getname()
現將上面的單線程改成多線程實現
本篇文章多線程的創建以及實現用runnable接口實現
(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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
import java.util.scanner; import com.sun.deploy.security.selectablesecuritymanager; import java.util.random; public class 微信發紅包多線程 { public static void main(string[] args) { scanner scanner= new scanner(system.in); int n; double money; system.out.println( "請輸入您想要發的紅包數量" ); n=scanner.nextint(); system.out.println( "請輸入您發送的紅包金額" ); money=scanner.nextdouble(); t3 t3= new t3(n,money); //創建runnable實現類的實例 for ( int j = 1 ; j <= n; j++) { new thread(t3, "紅包獲得者" + j).start(); //以上面創建的實例作為thread的參數來創建thread對象,并為thread對象指定一個名字,用線程對象的start方法來啟動該線程。 } } } class t3 implements runnable { //實現runnable接口 public double remain; //有紅包被領取后的余額 int n; //紅包數量 public synchronized void run() { //同步方法,在某一時刻只允許一個線程訪問,防止數據錯亂 rob(); } t3( int n, double money) { this .remain = money; this .n = n; } int a = n; public void rob() { double x2; if (n != 1 ) { //因為最后一個人領取金額為前面人領取紅包后剩下的,所以無需再進行隨機 x2 = process(); //取隨機金額 while (judge(x2) != 1 ) { //判斷取到的隨機金額是否非法,即是否能保證后來每個紅包領取者領到最低金額0.01 x2 = process(); //若非法則重新取隨機金額 } remain = remain - x2; //當領取成功后余額減去領走的金額 n--; //確保每次判斷人數為紅包數減一 } else { x2 = remain; //因為最后一個人領取金額為前面人領取紅包后剩下的,所以無需再進行隨機 string str = string.valueof(x2); string str1 = string.format( "%.2f" , x2); x2 = double .parsedouble(str1); } thread th = thread.currentthread(); //返回當前正在執行的線程對象 string th_name = th.getname(); //獲取線程的名稱 system.out.println(th_name + "搶到" + x2 + "元" ); } public int judge( double x) { //判斷函數 if (remain - x > (n - 1 ) * 0.01 ) { //確保后來紅包領取者能領到最低金額0.01 return 1 ; } else return 0 ; } public double process() { //實現紅包金額隨機的函數 double x2; double x1; string str1; random random = new random(); //隨機數為取0到1之間的任意double值 x1 = remain * random.nextdouble(); str1 = string.format( "%.2f" , x1); //轉化成字符串型用字符串型的format進行格式化處理,紅包金額最多取到小數點后兩位 x2 = double .parsedouble(str1); //再將字符串型數據轉換成double型 while (x2 == 0 ) { //如果所取金額非法則回爐重造 x1 = remain * random.nextdouble(); str1 = string.format( "%.2f" , x1); //轉化成字符串型用字符串型的format進行格式化處理,紅包金額最多取到小數點后兩位 x2 = double .parsedouble(str1); //再將字符串型數據轉換成double型 } return x2; } } |
程序運行結果如下:
(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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
import java.util.random; import java.util.scanner; public class 簡易微信發紅包多線程 2 { public static void main(string[] args) { scanner scanner= new scanner(system.in); double money= 0 ; //紅包總金額 int n; //紅包個數 system.out.println( "請輸入您想要發的紅包數量" ); n=scanner.nextint(); system.out.println( "請輸入您發送的紅包金額" ); money=scanner.nextdouble(); if (money/n== 0.01 ){ //當所發金額剛好為每人0.01元時 t4 t4= new t4(money,n); for ( int i= 1 ;i<=n;i++) { new thread(t4, "紅包獲得者" +i).start(); } } else { t1 t1= new t1(money,n); for ( int i= 1 ;i<=n;i++) { new thread(t1, "紅包獲得者" +i).start(); } } } } class t1 implements runnable{ double remain; int n; t1( double money, int n){ this .remain=money; this .n=n; } @override public synchronized void run() { rob(); } public void rob(){ double max; //最大可領紅包金額 double x1; //隨機金額 double x2; //所得金額 if (n!= 1 ) { //前n-1個紅包領取者領的紅包為隨機金額紅包 max=remain-(n- 1 )* 0.01 ; //最大可領紅包金額為剩下的人都獲得最小金額0.01 random random= new random(); x1=( double )random.nextint(( int ) ((max- 0.01 )* 100 )); //用nextint而不用nextdouble的原因是nextdouble無法設置seed //上式中max-0.01,下面的x2+0.01即解決了隨機數取0導致紅包獲得者沒搶到錢的問題 x1/= 100.0 ; x2=x1+ 0.01 ; remain=remain-x2; n=n- 1 ; thread th=thread.currentthread(); //獲取當前線程 string th_name=th.getname(); //獲取線程名字 system.out.println(th_name+ "獲取金額為:" +string.format( "%.2f" , x2)+ "元" ); } else { //最后一人領的紅包為前n-1個人領完后剩下的紅包 thread th=thread.currentthread(); //獲取當前線程 string th_name=th.getname(); //獲取線程名字 system.out.println(th_name+ "獲取金額為:" +string.format( "%.2f" , remain)+ "元" ); } } } class t4 implements runnable{ double remain; int n; t4( double money, int n){ this .remain=money; this .n=n; } public synchronized void run() { rob(); } public void rob(){ thread th=thread.currentthread(); //獲取當前線程 string th_name=th.getname(); //獲取線程名字 system.out.println(th_name+ "獲取金額為:" +string.format( "%.2f" , remain/n)+ "元" ); } } |
程序運行結果如下:
(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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
import java.util.random; import java.util.scanner; public class 簡易微信發紅包多線程 3 { public static void main(string[] args) { int p,n; double money; system.out.println( "請輸入您發送的紅包金額" ); scanner scanner= new scanner(system.in); money=scanner.nextdouble(); system.out.println( "請輸入您發送的紅包數量" ); n=scanner.nextint(); system.out.println( "請輸入參與搶紅包的人數" ); p=scanner.nextint(); hh hh= new hh(money,n); for ( int i= 1 ;i<=p;i++){ new thread(hh, "第" +i+ "個人" ).start(); } } } class hh implements runnable{ double money; int n; int count = 0 ; //計數器 double remain; hh( double money, int n){ this .money=money; //總金額 this .n=n; //紅包數 this .remain=money; //所剩金額 } @override public synchronized void run() { rob(); } public void rob(){ double x1,x2,d; string s1,s2; thread th=thread.currentthread(); //獲取當前線程 string th_name=th.getname(); //獲取線程名字 random random= new random(); d=money/(n- 1 ); //設置范圍讓每次所得金額不超過總數的1/(n-1),這樣也就避免了一次取得過大導致后面搶的紅包不能保證每個最少0.01 x1=d*random.nextdouble(); s1=string.format( "%.2f" ,x1); //轉化成字符串型用字符串型的format進行格式化處理,紅包金額最多取到小數點后兩位 x1 = double .parsedouble(s1); //再將字符串型數據轉換成double型 while (x1== 0 ||x1==money/(n- 1 )){ x1=d*random.nextdouble(); s1=string.format( "%.2f" ,x1); //轉化成字符串型用字符串型的format進行格式化處理,紅包金額最多取到小數點后兩位 x1 = double .parsedouble(s1); //再將字符串型數據轉換成double型 } s2= string.format( "%.2f" ,remain); //轉化成字符串型用字符串型的format進行格式化處理,紅包金額最多取到小數點后兩位 remain = double .parsedouble(s2); //再將字符串型數據轉換成double型 if (count<n- 1 ){ //前n-1個紅包金額為隨機金額 system.out.println(th_name+ "搶到了" +s1+ "元" ); remain-=x1; count++; } else if (count==n- 1 ){ //第n個為前n-1個紅包搶完所剩金額 system.out.println(th_name+ "搶到了" +s2+ "元" ); count++; } else if (count>n- 1 ){ //紅包被搶完后再來的 system.out.println(th_name+ "哎呀,手慢了!沒搶到!" ); count++; } } } |
程序運行結果如下:
總結
新手上路,因能力有限,若有不足之處還望大家海涵!
到此這篇關于java多線程實現簡易微信發紅包的文章就介紹到這了,更多相關java多線程實現微信發紅包內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/weixin_46569912/article/details/113308600