左右鞋的java多线程,哪位大师帮忙优化一下?

java 专栏收录该内容
140 篇文章 0 订阅
22个线程 1个打包 1个指挥

20个负责生产左右鞋.

左鞋300ms 右鞋700ms

优化的极限是1195

哪位大师帮忙优化一下?

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

interface Shoe {

}

class LShoe implements Shoe {

}

class RShoe implements Shoe {

}

class PoisonShoe implements Shoe {

}

class Pipeline implements Runnable {

private int LcostMS;
private int RcostMS;

private boolean stopFlag;

private BlockingQueue<Shoe> Lbucket;
private BlockingQueue<Shoe> Rbucket;

private String lineName;

public Pipeline(int LcostMS, int RcostMS, BlockingQueue<Shoe> Lbucket,
BlockingQueue<Shoe> Rbucket, String LR) {
this.LcostMS = LcostMS;
this.RcostMS = RcostMS;
this.stopFlag = false;
this.Lbucket = Lbucket;
this.Rbucket = Rbucket;
this.lineName = LR;
}

public String getLineName() {
return lineName;
}

public void setLineName(String lineName) {
this.lineName = lineName;
}

public boolean isStopFlag() {
return stopFlag;
}

public void setStopFlag(boolean stopFlag) {
this.stopFlag = stopFlag;
}

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

private void exec() {
try {
while (true) {

if (stopFlag) {
break;
}
// System.out.println(new Date());
innerRun();

}

} catch (InterruptedException e) {
e.printStackTrace();
} catch (Throwable e1) {
e1.printStackTrace();
}
}

private void costCPU(int costMS) throws InterruptedException {
Thread.sleep(costMS);
}

protected void innerRun() throws InterruptedException {

if (this.lineName.equals("L")) {
costCPU(this.LcostMS);
Lbucket.put(new LShoe());
// System.out.println("innerRun : L :" + bucket.size());

} else if (this.lineName.equals("R")) {
costCPU(this.RcostMS);
Rbucket.put(new RShoe());
// System.out.println("innerRun : R :" + bucket.size());

} else {
System.out.println("ERROR : lineName ");
}

}
}

class Packer implements Runnable {

private BlockingQueue<Shoe> Lbucket;
private BlockingQueue<Shoe> Rbucket;

private List<Pipeline> pList;
public int count = 0;

public Packer(BlockingQueue<Shoe> Lbucket, BlockingQueue<Shoe> Rbucket,
List<Pipeline> pList) {
this.Lbucket = Lbucket;
this.Rbucket = Rbucket;
this.pList = pList;
}

@Override
public void run() {
while (true) {
try {
if (LRShoesFactory.appStopFalg) {
break;
}
Shoe ls = this.Lbucket.take();
Shoe rs = this.Rbucket.take();

if (ls instanceof PoisonShoe || rs instanceof PoisonShoe) {
break;
}

if (!(ls instanceof LShoe) || !(rs instanceof RShoe)) {
System.out.println("ERROR-Thread-NO-SAFE");
}

System.out.println("Count : " + count + " bucket : L :"
+ Lbucket.size() + " R : " + Rbucket.size()
+ " Thread: " + LRShoesFactory.statistics(pList));
count++;
} catch (InterruptedException e) {
e.printStackTrace();
break;
}
}
}

}

class Commander implements Runnable {

private List<Pipeline> pList;
private BlockingQueue<Shoe> Lbucket;
private BlockingQueue<Shoe> Rbucket;
private boolean stopFlag;

public boolean isStopFlag() {
return stopFlag;
}

public void setStopFlag(boolean stopFlag) {
this.stopFlag = stopFlag;
}

public Commander(List<Pipeline> pList, BlockingQueue<Shoe> Lbucket,
BlockingQueue<Shoe> Rbucket) {
this.pList = pList;
this.Lbucket = Lbucket;
this.Rbucket = Rbucket;
}

@Override
public void run() {
while (true) {
if (LRShoesFactory.appStopFalg) {
break;
}
if (Lbucket.size() <= 3) {
LRShoesFactory.adjust(pList, "L", 1);
} else if (Rbucket.size() <= 1) {
LRShoesFactory.adjust(pList, "R", 3);
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
break;
}
}
}
}

public class LRShoesFactory {
public static boolean appStopFalg;

public static BlockingQueue<Shoe> Lbucket = new ArrayBlockingQueue<Shoe>(
100);

public static BlockingQueue<Shoe> Rbucket = new ArrayBlockingQueue<Shoe>(
100);

public static int LCost = 300;

public static int RCost = 700;

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

long starts = System.currentTimeMillis();
long ends = 0L;

List<Thread> tList = new ArrayList<Thread>();
List<Pipeline> pList = new ArrayList<Pipeline>();
Packer per = new Packer(Lbucket, Rbucket, pList);
Thread tp = new Thread(per);
tp.start();

Commander comer = new Commander(pList, Lbucket, Rbucket);
Thread tcomer = new Thread(comer);
tcomer.start();
while (true) {
if (tList.size() < 20) {
Thread t = null;
if (tList.size() % 3 == 0) {
Pipeline ler = new Pipeline(LCost, RCost, Lbucket, Rbucket,
"L");
t = new Thread(ler);
pList.add(ler);

} else {
Pipeline rer = new Pipeline(LCost, RCost, Lbucket, Rbucket,
"R");
t = new Thread(rer);
pList.add(rer);
}
t.start();
tList.add(t);

} else {
Thread.sleep(100);
ends = System.currentTimeMillis();
if ((ends - starts) >= 60000) {
stopAll(pList, Lbucket, Rbucket);
break;
}
}

}
System.out.println("const : " + (ends - starts));
System.out.println("ends : Main");

}

private static void stopAll(List<Pipeline> pList,
BlockingQueue<Shoe> Lbucket, BlockingQueue<Shoe> Rbucket)
throws InterruptedException {
appStopFalg = true;
for (int i = 0; i < pList.size(); i++) {
pList.get(i).setStopFlag(appStopFalg);
}
PoisonShoe ps = new PoisonShoe();
Lbucket.put(ps);
Rbucket.put(ps);

}

public static String statistics(List<Pipeline> pList) {

int L = 0;
int R = 0;
for (int i = 0; i < pList.size(); i++) {
Pipeline p = pList.get(i);
if (p.getLineName().equals("L")) {
L++;
}
if (p.getLineName().equals("R")) {
R++;
}
}
return "statistics : L : " + L + " R : " + R;

}

public static int getStatNum(List<Pipeline> pList, String LR) {

int L = 0;
int R = 0;
for (int i = 0; i < pList.size(); i++) {
Pipeline p = pList.get(i);
if (p.getLineName().equals("L")) {
L++;
}
if (p.getLineName().equals("R")) {
R++;
}
}
if (LR.equals("L")) {
return L;
} else if (LR.equals("R")) {
return R;
} else {
return -1;
}

}

public static void adjust(List<Pipeline> pList, String LR, int num) {
for (int t = 0; t < num; t++) {
if (LR.equals("L")) {
if (getStatNum(pList, "R") <= 2) {
break;
}
for (int i = 0; i < pList.size(); i++) {
Pipeline p = pList.get(i);
if (p.getLineName().equals("R")) {
p.setLineName("L");
break;
}
}
}
if (LR.equals("R")) {
if (getStatNum(pList, "L") <= 4) {
break;
}
for (int i = 0; i < pList.size(); i++) {
Pipeline p = pList.get(i);
if (p.getLineName().equals("L")) {
p.setLineName("R");
break;
}
}
}
}
// System.out.println("adjust : " + statistics(pList));

}
}
  • 1
    点赞
  • 0
    评论
  • 0
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

参与评论 您还未登录,请先 登录 后发表或查看评论
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页

打赏作者

天天实习

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

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

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

打赏作者

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

抵扣说明:

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

余额充值