`
shixiaomu
  • 浏览: 375238 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

    博客分类:
  • 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<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));

}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics