- 浏览: 375238 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
lhbthanks:
楼主写的很多,也很实用,要是再增加一些描述就会更好了。
oracle 用户 从一个表空间 另一个表空间 -
wuhuajun:
private int _connectionMax = 51 ...
resin jboss 最大连接数设置 -
shixiaomu:
自己丁丁丁一下 学了忘忘了再学。。主要是应用场景太少
python -
shixiaomu:
我自己有了方案了java+rabbitmq_server-2. ...
hadoop hive zookeeper 还不够 -
shixiaomu:
看到这个帖子 羞愧极了 ,原来 我 09 年就想学 pytho ...
python
左右鞋的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));
}
}
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));
}
}
发表评论
-
Java内部类的使用小结
2014-05-30 10:00 811内部类是指在一个外部类的内部再定义一个类。类名不需要和文件夹相 ... -
一种高效无锁内存队列的实现
2013-02-05 18:21 5282Disruptor是LMAX公司开源的一个高效的内存无锁队列 ... -
java io nio 区别和联系.
2013-02-05 18:08 4218IO ... -
IntelliJ
2012-11-19 16:07 776使用了一段时间的Intelli ... -
利用 org.apache.commons.io.FileUtils快速读写文件(转)
2012-11-07 12:25 2853利用 org.apache.commons.io.FileUt ... -
google 集合类 -loadcache-弱引用
2012-10-31 17:45 868google 集合类 l ... -
Shuffle-我能找到的最详细的文档-(转)
2012-09-11 16:02 853MapReduce:详解Shuffle过程 ... -
自动化运维系统雏形
2012-09-07 17:43 959java+python+robbitMQ+subprocess ... -
hadoop-map-reduce执行流程调研报告
2012-08-23 17:02 1187hadoop-map-reduce执行流程调研报告 参与者: ... -
hadoop hive zookeeper 还不够
2012-07-24 18:03 1303hadoop hive zookeeper 还不够 好久不 ... -
java try catchfinaly throws throw return 关系 终于搞明白了.
2012-02-24 01:40 3532package com.easou.cas06proxytes ... -
java-exception in thread “main”java.lang.NoSuchMethodError。
2011-07-15 09:37 950可能出现的情况是:有两个包-一个包里面有类,另一个包里面有部分 ... -
mina的深入学习-未完待续
2011-03-21 22:25 1052想要看懂mina的源代码,需要做一些知识储备. 我大体列一下: ... -
java 多线程 心得 体会
2011-03-03 10:12 1219点解 java 多线程.. 先 ... -
java 多线程模型--Future-原理及初步实现
2011-02-28 16:54 2904整理1: 什么是Future? ... -
java jar包大全.
2011-02-22 10:31 1194maven的另类用法. http://repo1.maven. ... -
程序员的路.....
2011-02-18 11:13 1588关于工作:关于挣钱:关于发展. 我的一些感悟: ... -
linux - resin 至强 参数配置
2011-02-12 10:02 1484/usr/local/vstore/jdk1.6.0_12/b ... -
resin jboss 最大连接数设置
2011-01-04 17:29 4254在近日的测试中发现,无论resin.conf中配置的并发连接数 ... -
名称解释+线路图
2010-12-13 16:58 919jmx --------------------------- ...
相关推荐
Java多线程设计模式上传文件Java多线程设计模式上传文件Java多线程设计模式上传文件Java多线程设计模式上传文件Java多线程设计模式上传文件Java多线程设计模式上传文件Java多线程设计模式上传文件Java多线程设计模式...
Java多线程读大文件 java多线程写文件:多线程往队列中写入数据
java多线程PPT 多线程基本概念 创建线程的方式 线程的挂起与唤醒 多线程问题
一个java 多线程操作数据库应用程序!!!
多线程启动.java多线程启动.java多线程启动.java多线程启动.java
详细的讲解了java多线程的原理,并配有代码进行实战,适合java初学者和想对多线程有进一步了解的人。
java多线程经典案例,线程同步、线程通信、线程阻塞等经典案例
轻松解决普通poi形式导出Excel的中出现的栈溢出问题,此资源可实现千万级数据分批导出csv文件,csv大数据量导出(千万级别,不会内存溢出),多线程导出 ,生产环境已经很稳定的使用着
该文档总结了Java多线程相关的知识点,分享给大家,简单易懂!
java多线程并发查询数据库,使用线程池控制分页,并发查询。
java多线程处理数据库数据,使用并发包,无框架,可批量处数据库数据,进行增删改。。等等操作。
资深Java专家10年经验总结,全程案例式讲解,首本全面介绍Java多线程编程技术的专著 结合大量实例,全面讲解Java多线程编程中的并发访问、线程间通信、锁等最难突破的核心技术与应用实践 Java多线程无处不在,如...
java多线程进度条
java多线程处理大数据,可根据配置的线程数,任务去调度处理
java 多线程 其实就是每个线程都拥有自己的内存空间,多线程之间的通信,比例A线程修改了主内存(main方法的线程)变量,需要把A线程修改的结果同步到主线程中,这时B线程再从主线程获取该变量的值,这样就实现了...
java多线程,对多线程,线程池进行封装,方便使用
Java多线程机制 9.1 Java中的线程 9.2 Thread的子类创建线程 9.3 使用Runable接口 9.4 线程的常用方法 9.5 GUI线程 9.6 线程同步 9.7 在同步方法中使用wait()、notify 和notifyAll()方法 9.8 挂起、恢复和终止线程 ...
java多线程并发的在新窗口
自己写的一些多线程的小Demo,在这里与大家分享一下。
java多线程文件传输 java多线程文件传输