JAVA多線程搶紅包的實現(xiàn)示例
大體思路
紅包的分發(fā)見JAVA作業(yè)——紅包分發(fā)。
而搶紅包要解決的是線程問題。
其實比較簡單,設(shè)定好人數(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è)置紅包個數(shù):"); red_pocket_num = scanner.nextInt(); System.out.println("請設(shè)置總金額數(shù)量(分):"); sum_money = scanner.nextInt(); if(sum_money < red_pocket_num) { System.out.println("錢不夠,退出程序。"); return; } System.out.println("請設(shè)置搶紅包成員個數(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; } }
到此這篇關(guān)于JAVA多線程搶紅包的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)JAVA多線程搶紅包內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Java實現(xiàn)UDP多線程在線咨詢
- Java并發(fā)編程之線程之間的共享和協(xié)作
- 基于Java網(wǎng)絡(luò)編程和多線程的多對多聊天系統(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結(jié)束線程的三種方法及該如何選擇
- Java中內(nèi)核線程理論及實例詳解
- Java線程數(shù)究竟設(shè)多少合理
相關(guān)文章
java中對象轉(zhuǎn)json字符串的幾種常用方式舉例
這篇文章主要給大家介紹了關(guān)于java中對象轉(zhuǎn)json字符串的幾種常用方式,在Java中可以使用許多庫將對象轉(zhuǎn)換為JSON字符串,其中最常用的是Jackson和Gson,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2023-10-10舉例講解設(shè)計模式中的訪問者模式在Java編程中的運用
這篇文章主要介紹了舉例講解設(shè)計模式中的訪問者模式在Java編程中的運用,訪問者模式是一種將算法與對象結(jié)構(gòu)分離的軟件設(shè)計模式,需要的朋友可以參考下2016-05-05MyBatis Map結(jié)果的Key轉(zhuǎn)為駝峰式
今天小編就為大家分享一篇關(guān)于MyBatis Map結(jié)果的Key轉(zhuǎn)為駝峰式,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12使用springboot activiti關(guān)閉驗證自動部署方式
這篇文章主要介紹了使用springboot activiti關(guān)閉驗證自動部署方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09詳解SpringMVC中的@RequestMapping注解
這篇文章主要介紹了SpringMVC中@RequestMapping注解,@RequestMapping注解是一個用來處理請求地址映射的注解,可用于映射一個請求或一個方法,可以用在類或方法上,需要的朋友可以參考下2023-07-07