JavaExp10:多线程设计,彩票购票问题

Posted 咳咳n

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaExp10:多线程设计,彩票购票问题相关的知识,希望对你有一定的参考价值。

1. 编写10个线程,第一个线程从1加到10,第二个线程从11加到20……第十个线程从91加到100,main函数最后再把10个线程的结果相加显示。

package EXP2020.exp10;

/**
 * @author JM
 * @date 2020/5/13
 */
/**创建继承于Thread类的线程子类*/
class ThreadDemo extends Thread 
    public int num;
    //sum记录当前线程结果之和,Sum记录所有子线程结果相加之和
    public static int sum, Sum;

    //定义构造函数
    public ThreadDemo(int num) 
        this.num = num;
    

    //重写run,实现累加求和
    public void run() 
        sum = 0;
        for (int i = num; i < num + 10; i++) 
            sum += i;
        
        System.out.println(this.getName() + "的和:" + sum);
        Sum += sum;//每个线程的结果再累加
    


/**主类*/
public class MainTestOne 
    public static void main(String[] args) throws InterruptedException //join会抛异常
        //创建10个线程
        Thread[] threads = new Thread[10];

        for (int i = 0; i < 10; i++) 
            threads[i] = new ThreadDemo(i * 10 + 1);
            threads[i].setName("线程" + (i + 1) + "");
            threads[i].start();
            //将10个子线程加入到主线程中,并且主线程会等待子线程结束之后才运行
            threads[i].join();
        

        //最后输出所有线程总和结果
        System.out.println("10个线程结果相加为:" + ThreadDemo.Sum);
    



2. 利用多线程实现彩票模拟程序:

package EXP2020.exp10;

/**
 * @author JM
 * @date 2020/5/13
 */

import java.util.ArrayList;
import java.util.Collection;

/**
 * 创建一个由产生彩票者和取彩票者共用的一个存储彩票类
 */
class Tickets 
    private int[] windows = new int[10];    //设置取彩票的窗口供人们取彩票
    private static int ticketsCount;        //产生或取出彩票的数目
    private int inPos, outPos;              //把彩票存入和取出时对应窗口的下标

    //用于存放4个消费者取到的彩票
    Collection c1 = new ArrayList();
    Collection c2 = new ArrayList();
    Collection c3 = new ArrayList();
    Collection c4 = new ArrayList();

    //传入彩票的编号
    public synchronized void put(int ticketsNum) 

        while (ticketsCount == windows.length) 
            try 
                this.wait();
             catch (InterruptedException e) 
                e.printStackTrace();
            
        
        windows[inPos] = ticketsNum;
        System.out.println("发放彩票中......当前发放的彩票号码为:" + windows[inPos]);
        inPos = (1 + inPos) % windows.length;
        ticketsCount++;
        this.notify();

    

    //取彩票
    public synchronized void get() 
        while (ticketsCount == 0) 
            try 
                this.wait();
             catch (InterruptedException e) 
                e.printStackTrace();
            
        
        int data = windows[outPos];    // 从数组中取出数据
        Recode(data);
        System.out.println(Thread.currentThread().getName() + "从窗口" + (outPos + 1) + "中取出号码为:" + data + "的彩票");
        windows[outPos] = 0;
        outPos = (outPos + 1) % windows.length;
        ticketsCount--;
        this.notifyAll();
    

    //记录每个取票者取到的彩票信息,保存到容器
    public void Recode(int data) 
        if (Thread.currentThread().getName().equals("取票者1号"))
            c1.add(data);
        else if (Thread.currentThread().getName().equals("取票者2号"))
            c2.add(data);
        else if (Thread.currentThread().getName().equals("取票者3号"))
            c3.add(data);
        else if (Thread.currentThread().getName().equals("取票者4号"))
            c4.add(data);
    


/**
 * 生产彩票的线程类
 */
class ProduceTickets implements Runnable 
    private Tickets tickets;
    protected static int prizeNum = 6666;   //设置中奖号码为6666,也可以用Random产生
    private int ticketNum = 1;              //设第一张彩票号码为1


    ProduceTickets(Tickets tickets) 
        this.tickets = tickets;
    

    public void run() 
        while (ticketNum <= 10000) 
            tickets.put(ticketNum++);
        
        System.out.println("票数已经发放完");
    


/**
 * 取彩票线程类
 */
class ConsumeTicket implements Runnable 
    private Tickets tickets;

    ConsumeTicket(Tickets tickets) 
        this.tickets = tickets;
    

    public void run() 
        while (true) 
            tickets.get();
        
    


public class MainTestTwo 
    public static void main(String[] args) throws InterruptedException 

        //创建共用的彩票存储空间对象
        Tickets tickets = new Tickets();

        //创建生产和消费两个目标对象,传入tickets对象
        ProduceTickets producer = new ProduceTickets(tickets);
        ConsumeTicket consumes = new ConsumeTicket(tickets);

        //创建4个取彩票线程
        Thread[] threads = new Thread[4];

        System.out.println("..........................开始发放彩票.............................");

        //启动生产彩票的线程和取彩票者的线程
        Thread prod = new Thread(producer);
        prod.start();
        for (int i = 0; i < 4; i++) 
            threads[i] = new Thread(consumes);
            threads[i].setName("取票者" + (i + 1) + "号");
            threads[i].start();
        

        Thread.sleep(2000);
        System.out.println(".........................所有彩票已经销售完.................................");

        //公布中奖号码
        System.out.println("中奖彩票号码是:" + ProduceTickets.prizeNum);

        //分别输出4个取票者取到的彩票
        Collection[] c = tickets.c1, tickets.c2, tickets.c3, tickets.c4;
        for (int i = 0; i < 4; i++) 
            System.out.println("取票者" + (i + 1) + "号取到的彩票有:");
            System.out.println(c[i]);
        

        //公布中奖的人
        for (int i = 0; i < 4; i++)
            if (c[i].contains(ProduceTickets.prizeNum))
                System.out.println("中奖的是:取票者" + (i + 1) + "号");


        System.exit(0);
        prod.join();
        for (int i = 0; i < 4; i++) 
            threads[i].join();
        
    





向右拉,验证成功

以上是关于JavaExp10:多线程设计,彩票购票问题的主要内容,如果未能解决你的问题,请参考以下文章

JavaExp12:网络编程

JavaExp12:网络编程

多线程---网络购票

多线程13--多线程并发购票--低效的线程安全的车票容器

一个简单多线程购票Demo

Thread:多线程 实现简单购票