Java的多线程运行程序编写

Java的多线程运行程序编写

现有存有鞋品信息的字符串"k1943 鳄鱼恤男式皮鞋 A 295 57 40 黑,k2144 杰尊男鞋子 A 149 53 42 黑,k2148 求足女式运动鞋 B 80 195 36 白,k2146 嘉顿女式运动鞋 B 85 256 37 灰,c2954 飞腾女式凉鞋 C 15 558 36 红,c2578 胭脂女式皮鞋 A 150 53 37 红";各鞋品信息之间使用英文逗号分隔,每个鞋品包含了编号、品牌、类别、单价、库存、尺码、颜色等信息。

1)根据鞋品信息,写出类shoes。

2)将这些鞋品信息保存到某集合中。

3)编写多线程程序,模拟三个销售人员(张某、吴某、魏某)共同销售鞋品信息,但不能重复销售鞋品。某次运行时模拟销售输出的鞋品信息:

(1)shoesdata.txt

k1943 鳄鱼恤男式皮鞋 A 295 57 40 黑
k2144 杰尊男鞋子 A  149 53 42 黑
k2148 求足女式运动鞋 B 80 195 36 白
k2146 嘉顿女式运动鞋 B 85 256 37 灰
c2954 飞腾女式凉鞋 C 15 558 36 红
c2578 胭脂女式皮鞋 A 150 53 37

(2)Shoes.java

import java.io.Serializable;

public class Shoes implements Serializable{
   private String no;
   private String name;
   private String zhiliang;//质量
   private float price;
   private int size1;
   private int size2;
   private String color;

    public Shoes() {
    }

    public Shoes(String no, String name, String zhiliang, float price, int size1, int size2, String color) {
        this.no = no;
        this.name = name;
        this.zhiliang = zhiliang;
        this.price = price;
        this.size1 = size1;
        this.size2 = size2;
        this.color = color;
    }

    public String getNo() {
        return no;
    }

    public void setNo(String no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getZhiliang() {
        return zhiliang;
    }

    public void setZhiliang(String zhiliang) {
        this.zhiliang = zhiliang;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }

    public int getSize1() {
        return size1;
    }

    public void setSize1(int size1) {
        this.size1 = size1;
    }

    public int getSize2() {
        return size2;
    }

    public void setSize2(int size2) {
        this.size2 = size2;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    @Override
    public String toString() {
        return no+"\t"+name+"\t"+zhiliang+"\t"+price+"元"+"\t"+size1+"\t"+size2;
    }
   
}

(3)ReadShoes.java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class ReadShoes {
    public static ArrayList<Shoes> readShoes(){
        ArrayList<Shoes> arr=new ArrayList<Shoes>();
        try {
            Scanner sc=new Scanner(new File("src\\shoesdata.txt"));
            while(sc.hasNext()){
               Shoes s=new Shoes(sc.next(),sc.next(),sc.next(),sc.nextFloat(),sc.nextInt(),sc.nextInt(),sc.next());
               arr.add(s);
           }       
        } catch (FileNotFoundException ex) {
            System.out.println("文件不存在!");
        }
        return arr;
    }
}

(4)ThreadShoes.java

import java.util.ArrayList;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ThreadShoes implements Runnable{
    private static ArrayList<Shoes> ar=new ArrayList<Shoes>();
    private ArrayList<Integer> saled=new ArrayList<Integer>();

    public ThreadShoes() {
        ar=ReadShoes.readShoes();  
    }

    @Override
    public void run() {
        Saler();
    }

    private void Saler() {
       Random rd=new Random();
       Integer k=0;
        while(saled.size()<ar.size()){              
       synchronized(this) {    
        while(saled.contains(k)){
                  k=rd.nextInt(ar.size());                   
            }
                saled.add(k);
              System.out.println(Thread.currentThread().getName()+"卖出去了:"+ar.get(k));
       }  
           try {    
               Thread.sleep(500);
           } catch (InterruptedException ex) {              
           }
       }
    }    
}

(5)ShoesThread.java

public class ShoesThread {
    public static void main(String[] args) {
        ThreadShoes ts=new ThreadShoes();
        Thread t1=new Thread(ts,"张某");
        Thread t2=new Thread(ts,"吴某");
        Thread t3=new Thread(ts,"魏某");
        t1.start();
        t2.start();
        t3.start();
    }
}

输出结果
在这里插入图片描述

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

v_wus

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值