JAVA多線程搶紅包的實現(xiàn)示例
更新時間:2021年03月29日 11:34:38 作者:Evrse
這篇文章主要介紹了JAVA多線程搶紅包的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
大體思路
紅包的分發(fā)見JAVA作業(yè)——紅包分發(fā)。
而搶紅包要解決的是線程問題。
其實比較簡單,設定好人數(shù),每個人一個線程,每個線程執(zhí)行一遍,有紅包就搶,沒有紅包就搶不到,所以run函數(shù)中只要判斷現(xiàn)在還有沒有紅包就可以了。
代碼實現(xiàn)
import java.util.Random; import java.util.Scanner; public class Main { public static void main(String[] args) { int person_num, red_pocket_num, sum_money; Scanner scanner = new Scanner(System.in); System.out.println("請設置紅包個數(shù):"); red_pocket_num = scanner.nextInt(); System.out.println("請設置總金額數(shù)量(分):"); sum_money = scanner.nextInt(); if(sum_money < red_pocket_num) { System.out.println("錢不夠,退出程序。"); return; } System.out.println("請設置搶紅包成員個數(shù):"); person_num = scanner.nextInt(); myRunnable myrunnable = new myRunnable(sum_money,red_pocket_num); Thread []person = new Thread[person_num]; for (int i = 0; i < person_num; i++) { person[i] = new Thread(myrunnable); person[i].setName("用戶"+(i+1)); person[i].start(); } } } class myRunnable implements Runnable{ private int []red_pocket; private int num; private int now_num; public myRunnable(int money, int num) { this.red_pocket = new Red_Pocket(money, num).get_red_packets(); this.num = num; this.now_num = num; } @Override public void run() { if(this.num>0){ System.out.println(Thread.currentThread().getName()+"搶到了紅包 "+(this.num-this.now_num+1)+" : "+red_pocket[--this.now_num]+"分"); } else{ System.out.println(Thread.currentThread().getName()+"未搶到紅包。"); } } } class Red_Pocket{ private long seed; private int money; private int num; public int[] get_red_packets() { if(this.money < this.num) return new int[0]; Random random = new Random(this.seed); this.seed = random.nextLong(); int[] res = new int[this.num]; double[] temp = new double[this.num]; double sum = 0; int sum2 = 0; for (int i = 0; i < this.num; i++) { temp[i] = random.nextDouble(); sum += temp[i]; } for (int i = 0; i < this.num; i++) { res[i] = 1 + (int)(temp[i] / sum * (this.money - this.num)); sum2 += res[i]; } res[random.nextInt(this.num)] += this.money - sum2; return res; } private void init() { this.seed = new Random(System.currentTimeMillis()).nextLong(); } public Red_Pocket(int money,int num) { init(); this.money = money; this.num = num; } }
到此這篇關于JAVA多線程搶紅包的實現(xiàn)示例的文章就介紹到這了,更多相關JAVA多線程搶紅包內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
- Java實現(xiàn)UDP多線程在線咨詢
- Java并發(fā)編程之線程之間的共享和協(xié)作
- 基于Java網(wǎng)絡編程和多線程的多對多聊天系統(tǒng)
- java的多線程高并發(fā)詳解
- 淺談JAVA 線程狀態(tài)中可能存在的一些誤區(qū)
- 教你如何使用Java多線程編程LockSupport工具類
- Java多線程之線程池七個參數(shù)詳解
- Java利用線程工廠監(jiān)控線程池的實現(xiàn)示例
- Java線程實現(xiàn)時間動態(tài)顯示
- Java 用兩個線程交替打印數(shù)字和字母
- Java多線程面試題(面試官常問)
- Java多線程下載網(wǎng)圖的完整案例
- Java結束線程的三種方法及該如何選擇
- Java中內核線程理論及實例詳解
- Java線程數(shù)究竟設多少合理
相關文章
使用springboot activiti關閉驗證自動部署方式
這篇文章主要介紹了使用springboot activiti關閉驗證自動部署方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09詳解SpringMVC中的@RequestMapping注解
這篇文章主要介紹了SpringMVC中@RequestMapping注解,@RequestMapping注解是一個用來處理請求地址映射的注解,可用于映射一個請求或一個方法,可以用在類或方法上,需要的朋友可以參考下2023-07-07