ZKX's LAB

java多线程模拟抢红包功能

2020-07-16知识4
java多线程问题:模拟10个人在3个窗口买票的过程 run()方法不对,没有控制同步,如下修改:
public void run() { while(true) { synchronized(this){ if(number>0){ System.out.println(\"第\"+... JAVA多线程 模拟每隔一秒输入一个数据 就你这个例子来看,没必要多线程,因为Run方法很简单,处理耗时很短。如果非要多线程,可以维护一个共享的index变量,保证每个线程获得index时都不重复。class EMailList{ private Listlist;通过构造函数或setter赋值. private int index=-1;synchronized public int getIndex(){ return index+;} public Email getEMail(){ int i=getIndex();if(i()){ retur list.get(i);}else { return null;} } } class Run implements Runable{ private EMailList hander;通过构造函数赋值. public void run(){ EMail email=hander.getEMail();while(email!null){ email=hander.getEMail();} } } main方法中 EMailList hander=new EMailList(emailList);for(int i=0;i;i+){ Run run=new Run(hander);new Thread(run).start();} java多线程模拟售票问题 结果是对的,每一个售票的线程都有自己票数,所以每个线程都会售出十张票;如果你想让这五个售票线程只能售出十张票的话,那就 private?static?int?Ticketnum=10;... java模拟龟兔赛跑问题用多线程实现 import java.util.Date;public class Test extends Thread{ private int tortoise_walk=0;乌龟已跑长度存放变量 private int rabbit_walk=0;兔子已跑长度存放变量 private int finish=1000;终点 private volatile boolean hasWinner=false;胜利者诞生/* ClassName:Tortoise_Run Description:TODO(乌龟奔跑线程) author guotingchao date 2012-3-6 上午10:20:45 class Tortoise_Run implements Runnable { Override public void run(){ try { while!hasWinner){ if(tortoise_walk%100=0&(tortoise_walk!0|tortoise_walk>=finish)){/乌龟每100米休息500毫秒 System.out.println(\"乌龟休息中…\");Thread.sleep(500);} tortoise_walk+;System.out.println(\"乌龟已跑\"+tortoise_walk+\"米\");} } catch(InterruptedException e){ e.printStackTrace();} } }/* ClassName:Rabbit_Run Description:TODO(兔子奔跑线程) date 2012-3-6 上午10:25:10 author guotingchao class Rabbit_Run implements Runnable { Override public void run(){ try { while!hasWinner){ if(rabbit_walk%20=0&(rabbit_walk!0|rabbit_walk>=finish)){/兔子每20米休息500毫秒 System.out.println(... java 多线程的问题,生产者消费者的模拟,生成一个消费一个,那位大神帮忙,在线等 /Product.java public class Product { private static int counter;public void produce()throws InterruptedException { counter=(int)((Math.random()*1000)/10);System.out.println(\"生产产品->编号:\"+counter);} public void consume()throws InterruptedException { System.out.println(\"消费产品->编号:\"+counter);} } ProductList.java import java.util.*;public class ProductList { private static Listlist=new ArrayList();public void add(Product product)throws Exception {/增加到产品队列 list.add(product);} public Product remove()throws Exception {/返回提取产品,并从队列中删除 int no=list.size()-1;if(no)/产品队列为空;throw new Exception(\"产品队列为空\");Product product=list.get(no);list.remove(product);return product;} } Producer.java public class Producer implements Runnable { private ProductList productlist;public Producer(ProductList productlist){ this.productlist=productlist;} public void run(){ for(int i=0;i;i+){ try { Product product=new Product();product.produce();产品 productlist... ,用Java语言编写一个程序,用两个线程模拟对话,任何一个线程都可以随时收发信息. 就是个socket而已,自己网上搜,太多了;socket例子 java多线程模拟买票问题 用图形用户界面 能详细说明一下买票按钮的功能是什么吗程序有几个问题:第一,maipiao类名要首字母大写,变量的定义尽量不要使用中文第二,Sell A;没有初始化,然后你就开始了调用第三,public void actionPerformed(ActionEvent e),放在Maipiao类里面,什么时候被调用?第四,多线程的public void run()方法,同样在Maipiao类中,可是它没有被调用问题有不少,我实在修改不下去了,明天白天如果有空就帮你重写一个终于给你修改完了,要的话留邮箱 java问题 模拟抢红包过程 public class DD { static byte[]lock=new byte[1];static int hongbao=5;static boolean jx=true;public static void main(String args[])throws InterruptedException{ 7a686964616fe59b9ee7ad9431333339666665 MYT m1=new MYT(\"m1\");MYT m2=new MYT(\"m2\");MYT m3=new MYT(\"m3\");MYT m4=new MYT(\"m4\");MYT m5=new MYT(\"m5\");m1.start();m2.start();m3.start();m4.start();m5.start();} static class MYT extends Thread implements Runnable{ String name=\";public MYT(String n){ name=n;} Override public void run(){ while(jx){ DD.qiang(name);} } } public static void qiang(String name){ synchronized(lock){ if!jx)return;System.out.println(name+\"获得\"+(int)(Math.random()*100));if(-hongbao=0)jx=false;try { Thread.sleep(1000);} catch(InterruptedException e){ TODO 自动生成的 catch 块 e.printStackTrace();} } } } 多运行几次能看到不同的结果 java多线程模拟售票问题 结果是对的,每一个售票的线程都有自己票数,所以每个线程都会售出十张票;如果你想让这五个售票线程只能售出十张票的话,那就 private static int Ticketnum=10;设置成静态变量,这样的话,他就不属于实例了,而是属于整个类了 利用多线程技术模拟微信抢红包 要完整java程序 packagecom.lulei.weixin.util;import java.util.ArrayList;import java.util.List;importcom.lulei.util.JsonUtil;public class RedPacketUtil { 最小红包额度 private ...

#线程#list#多线程

随机阅读

qrcode
访问手机版