最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JAVA多線程搶紅包的實(shí)現(xiàn)示例

 更新時(shí)間:2021年03月29日 11:34:38   作者:Evrse  
這篇文章主要介紹了JAVA多線程搶紅包的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

大體思路

紅包的分發(fā)見(jiàn)JAVA作業(yè)——紅包分發(fā)。
而搶紅包要解決的是線程問(wèn)題。
其實(shí)比較簡(jiǎn)單,設(shè)定好人數(shù),每個(gè)人一個(gè)線程,每個(gè)線程執(zhí)行一遍,有紅包就搶,沒(méi)有紅包就搶不到,所以run函數(shù)中只要判斷現(xiàn)在還有沒(méi)有紅包就可以了。

代碼實(shí)現(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("請(qǐng)?jiān)O(shè)置紅包個(gè)數(shù):");
    red_pocket_num = scanner.nextInt();
    System.out.println("請(qǐng)?jiān)O(shè)置總金額數(shù)量(分):");
    sum_money = scanner.nextInt();
    if(sum_money < red_pocket_num) {
      System.out.println("錢不夠,退出程序。");
      return;
    }
    System.out.println("請(qǐng)?jiān)O(shè)置搶紅包成員個(gè)數(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多線程搶紅包的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)JAVA多線程搶紅包內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

沧州市| 滁州市| 巢湖市| 敖汉旗| 武功县| 合阳县| 长丰县| 集贤县| 浮山县| 西青区| 修文县| 河北省| 西安市| 苏尼特右旗| 九寨沟县| 武乡县| 义马市| 威海市| 天全县| 杨浦区| 乐都县| 惠水县| 宣威市| 平山县| 禹州市| 金秀| 芦溪县| 扶风县| 酒泉市| 九龙坡区| 望江县| 湟源县| 安西县| 威宁| 搜索| 辽宁省| 铁岭市| 建宁县| 鹤岗市| 准格尔旗| 吉林市|