- 浏览: 382396 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
lhbthanks:
楼主写的很多,也很实用,要是再增加一些描述就会更好了。
oracle 用户 从一个表空间 另一个表空间 -
wuhuajun:
private int _connectionMax = 51 ...
resin jboss 最大连接数设置 -
shixiaomu:
自己丁丁丁一下 学了忘忘了再学。。主要是应用场景太少
python -
shixiaomu:
我自己有了方案了java+rabbitmq_server-2. ...
hadoop hive zookeeper 还不够 -
shixiaomu:
看到这个帖子 羞愧极了 ,原来 我 09 年就想学 pytho ...
python
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 846内部类是指在一个外部类的内部再定义一个类。类名不需要和文件夹相 ... -
一种高效无锁内存队列的实现
2013-02-05 18:21 5336Disruptor是LMAX公司开源的一个高效的内存无锁队列 ... -
java io nio 区别和联系.
2013-02-05 18:08 4263IO ... -
IntelliJ
2012-11-19 16:07 816使用了一段时间的Intelli ... -
利用 org.apache.commons.io.FileUtils快速读写文件(转)
2012-11-07 12:25 2919利用 org.apache.commons.io.FileUt ... -
google 集合类 -loadcache-弱引用
2012-10-31 17:45 903google 集合类 l ... -
Shuffle-我能找到的最详细的文档-(转)
2012-09-11 16:02 894MapReduce:详解Shuffle过程 ... -
自动化运维系统雏形
2012-09-07 17:43 993java+python+robbitMQ+subprocess ... -
hadoop-map-reduce执行流程调研报告
2012-08-23 17:02 1227hadoop-map-reduce执行流程调研报告 参与者: ... -
hadoop hive zookeeper 还不够
2012-07-24 18:03 1357hadoop hive zookeeper 还不够 好久不 ... -
java try catchfinaly throws throw return 关系 终于搞明白了.
2012-02-24 01:40 3593package com.easou.cas06proxytes ... -
java-exception in thread “main”java.lang.NoSuchMethodError。
2011-07-15 09:37 986可能出现的情况是:有两个包-一个包里面有类,另一个包里面有部分 ... -
mina的深入学习-未完待续
2011-03-21 22:25 1089想要看懂mina的源代码,需要做一些知识储备. 我大体列一下: ... -
java 多线程 心得 体会
2011-03-03 10:12 1259点解 java 多线程.. 先 ... -
java 多线程模型--Future-原理及初步实现
2011-02-28 16:54 2942整理1: 什么是Future? ... -
java jar包大全.
2011-02-22 10:31 1227maven的另类用法. http://repo1.maven. ... -
程序员的路.....
2011-02-18 11:13 1624关于工作:关于挣钱:关于发展. 我的一些感悟: ... -
linux - resin 至强 参数配置
2011-02-12 10:02 1521/usr/local/vstore/jdk1.6.0_12/b ... -
resin jboss 最大连接数设置
2011-01-04 17:29 4288在近日的测试中发现,无论resin.conf中配置的并发连接数 ... -
名称解释+线路图
2010-12-13 16:58 941jmx --------------------------- ...
相关推荐
Java多线程设计模式上传文件Java多线程设计模式上传文件Java多线程设计模式上传文件Java多线程设计模式上传文件Java多线程设计模式上传文件Java多线程设计模式上传文件Java多线程设计模式上传文件Java多线程设计模式...
### Java多线程操作数据库:深入解析与应用 在当今高度并发的应用环境中,Java多线程技术被广泛应用于处理数据库操作,以提升系统的响应速度和处理能力。本文将基于一个具体的Java多线程操作数据库的应用程序,深入...
Java多线程导出Excel是处理大数据量时的一种高效策略,尤其在面对千万级别的数据时。传统的Apache POI库在处理大规模数据时可能会遇到栈溢出(StackOverflowError)和内存溢出(OutOfMemoryError)等问题,因为这些...
Java多线程读大文件 java多线程写文件:多线程往队列中写入数据
Java多线程是Java编程中的重要概念,尤其在如今的多核处理器环境下,理解并熟练掌握多线程技术对于提高程序性能和响应速度至关重要。本资料详细讲解了Java多线程的原理,并提供了丰富的实战代码,非常适合Java初学者...
### Java多线程分页查询知识点详解 #### 一、背景与需求分析 在实际的软件开发过程中,尤其是在处理大量数据时,如何高效地进行数据查询成为了一个关键问题。例如,在一个用户众多的社交平台上,当用户需要查看...
Java多线程是Java编程中的一个重要概念,它允许程序同时执行多个任务,提高了程序的效率和响应速度。在Java中,实现多线程有两种主要方式:继承Thread类和实现Runnable接口。 1. 继承Thread类: 当我们创建一个新...
在Java编程中,多线程查询数据库是一种常见的优化策略,特别是在处理大数据量或者需要并行执行多个查询时。本文将详细探讨如何利用Java的多线程技术和线程池来实现并发查询数据库,以及相关的文件`BatchDataUtil....
在Java编程中,多线程处理是提升程序性能和效率的重要手段,特别是在处理大量数据库数据时。本主题将深入探讨如何使用Java的并发包(java.util.concurrent)来实现多线程对数据库数据的批量处理,包括增、删、改等...
《JAVA多线程教学演示系统》是一篇深入探讨JAVA多线程编程的论文,它针对教育领域中的教学需求,提供了一种生动、直观的演示方式,帮助学生更好地理解和掌握多线程技术。这篇论文的核心内容可能包括以下几个方面: ...
在Java编程中,多线程并发是提升程序执行效率、充分利用多核处理器资源的重要手段。本文将基于"java 多线程并发实例"这个主题,深入探讨Java中的多线程并发概念及其应用。 首先,我们要了解Java中的线程。线程是...
在Java编程中,多线程技术是处理大数据批量导入或导出的重要手段。它能有效提升程序执行效率,尤其在数据库操作这样的I/O密集型任务中。本项目以"java多线程实现大批量数据导入源码"为题,旨在通过多线程策略将大量...
在Java编程领域,多线程是一项至关重要的技术,它允许程序同时执行多个任务,从而提高系统资源的利用率和程序的响应速度。这份“JAVA多线程编程技术PDF”是学习和掌握这一领域的经典资料,涵盖了多线程的全部知识点...
《Java多线程编程实战指南》这本书深入浅出地讲解了Java多线程的核心概念和实战技巧,分为核心篇和设计模式篇,旨在帮助开发者掌握并应用多线程技术。 1. **线程基础** - **线程的创建**:Java提供了两种创建线程...
《汪文君JAVA多线程编程实战》是一本专注于Java多线程编程的实战教程,由知名讲师汪文君倾力打造。这本书旨在帮助Java开发者深入理解和熟练掌握多线程编程技术,提升软件开发的效率和质量。在Java平台中,多线程是...
线程池是Java多线程处理的一种优化策略,通过复用已创建的线程来减少线程创建和销毁的开销。Java提供`ExecutorService`接口和`Executors`工厂类来创建线程池。常见的线程池类型有: 1. **FixedThreadPool**:固定...
java多线程处理大数据,可根据配置的线程数,任务去调度处理
什么是线程? 线程是进程中的最小执行单元,是进程内部的一次独立的控制流程。在Java中,每个进程至少有一个线程,即**主线程**。当Java程序启动时,JVM会创建主线程并在该线程中调用`main()`方法。除了主线程外,...
《Java多线程编程实战指南-核心篇》是一本深入探讨Java并发编程的书籍,旨在帮助读者掌握在Java环境中创建、管理和同步线程的核心技术。Java的多线程能力是其强大之处,使得开发者能够在同一时间执行多个任务,提高...
Java多线程测试工具在软件开发中扮演着至关重要的角色,尤其在性能优化和系统压力测试方面。本文将深入探讨Java多线程测试工具的重要性和使用,以"JMeter"为例,来阐述如何通过此类工具进行高效的压力测试和系统瓶颈...