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

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 Lbucket;
private BlockingQueue Rbucket;

private String lineName;

public Pipeline(int LcostMS, int RcostMS, BlockingQueue Lbucket,
BlockingQueue 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 Lbucket;
private BlockingQueue Rbucket;

private List pList;
public int count = 0;

public Packer(BlockingQueue Lbucket, BlockingQueue Rbucket,
List 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 pList;
private BlockingQueue Lbucket;
private BlockingQueue Rbucket;
private boolean stopFlag;

public boolean isStopFlag() {
return stopFlag;
}

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

public Commander(List pList, BlockingQueue Lbucket,
BlockingQueue 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 Lbucket = new ArrayBlockingQueue(
100);

public static BlockingQueue Rbucket = new ArrayBlockingQueue(
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 tList = new ArrayList();
List pList = new ArrayList();
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 pList,
BlockingQueue Lbucket, BlockingQueue 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 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 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 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));

}
}

你可能感兴趣的:(java)