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();
}
}
输出结果