- 浏览: 328631 次
- 性别:
- 来自: 天津
文章分类
最新评论
-
xing0029:
这样配置的话 事物不成功啊 还有什么地方需要注意的么 可以加我 ...
JTA集成JOTM或Atomikos配置分布式事务(Tomcat应用服务器) -
mengqingyu:
liuxiaolu 写道jotm的我的没有成功,楼主能否帮助一 ...
JTA集成JOTM或Atomikos配置分布式事务(Tomcat应用服务器) -
liuxiaolu:
jotm的我的没有成功,楼主能否帮助一下
JTA集成JOTM或Atomikos配置分布式事务(Tomcat应用服务器) -
aptech406328627:
求解救,没弄好QQ:1053942353
Spring邮件发送(可带附件,模板,群发,异步发送等功能) -
ghpaas:
web可视化自定义表单推荐使用GForms开发平台(http: ...
在线表单设计器设计原理
在公司做培训时用到,顺便在这里做个总结。
1.生产者消费者模式
某个模块负责产生数据,这些数据由另一个模块来负责处理(此处的模块是广义的,可以是类、函数、线程、进程等)。产生数据的模块,就形象地称为生产者;而处理数据的模块,就称为消费者。在生产者与消费者之间在加个缓冲区,我们形象的称之为仓库,生产者负责往仓库了进商品,而消费者负责从仓库里拿商品,这就构成了生产者消费者模式。
2.future模式
从java 5开始,Java提供了Callable接口,该接口是Runnable接口的增强版,Callable接口提供了一个call()方法,可以作为线程执行体,但call()方法比run()方法的功能更强大。
call()方法可以有返回值,call()方法可以声明抛出异常。
1.生产者消费者模式
某个模块负责产生数据,这些数据由另一个模块来负责处理(此处的模块是广义的,可以是类、函数、线程、进程等)。产生数据的模块,就形象地称为生产者;而处理数据的模块,就称为消费者。在生产者与消费者之间在加个缓冲区,我们形象的称之为仓库,生产者负责往仓库了进商品,而消费者负责从仓库里拿商品,这就构成了生产者消费者模式。
/** * * 类功能描述:数据对象 * * @author <a href="mailto:qingyu.meng21@gmail.com">mengqingyu </a> * @version $Id: codetemplates.xml,v 1.1 2009/03/06 01:13:01 mengqingyu Exp $ * Create: 2013-4-2 上午11:01:53 */ public class MyData { private final long intData; public MyData(long d){ intData = d; } @Override public String toString(){ return " MyData:"+intData; } } import java.util.concurrent.BlockingQueue; public abstract class AbstractPC implements Runnable { protected BlockingQueue<MyData> queue; protected volatile boolean isRunning = true; public void stop() { isRunning = false; } } import java.util.concurrent.BlockingQueue; import java.util.concurrent.atomic.AtomicInteger; /** * * 类功能描述:生产者 * * @author <a href="mailto:qingyu.meng21@gmail.com">mengqingyu </a> * @version $Id: codetemplates.xml,v 1.1 2009/03/06 01:13:01 mengqingyu Exp $ * Create: 2013-4-2 上午11:02:13 */ public class Producer extends AbstractPC{ private static AtomicInteger count = new AtomicInteger(0); private static final int SLEEP_TIME = 3000; public Producer(BlockingQueue<MyData> queue) { this.queue = queue; } public void run() { System.out.println("Producer:"+Thread.currentThread().getName()+" start"); try { while (isRunning) { Thread.sleep(SLEEP_TIME); MyData data = new MyData(count.incrementAndGet()); queue.put(data); System.out.println(Thread.currentThread().getName()+" produce:" + data); } } catch (InterruptedException e) { e.printStackTrace(); } } } import java.util.concurrent.BlockingQueue; /** * * 类功能描述:消费者 * * @author <a href="mailto:qingyu.meng21@gmail.com">mengqingyu </a> * @version $Id: codetemplates.xml,v 1.1 2009/03/06 01:13:01 mengqingyu Exp $ * Create: 2013-4-2 上午11:02:22 */ public class Consumer extends AbstractPC{ private static final int SLEEP_TIME = 3000; public Consumer(BlockingQueue<MyData> queue) { this.queue = queue; } public void run() { System.out.println("Consumer:"+Thread.currentThread().getName()+" start"); try { while(isRunning){ Thread.sleep(SLEEP_TIME); MyData data = queue.take(); if (null != data) { System.out.println(Thread.currentThread().getName()+" consume:"+data); } } } catch (InterruptedException e) { e.printStackTrace(); } } } import java.util.concurrent.BlockingQueue; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.LinkedBlockingQueue; /** * * 类功能描述:完全利用并发包的特性,最优的实现方式。 * * @author <a href="mailto:qingyu.meng21@gmail.com">mengqingyu </a> * @version $Id: codetemplates.xml,v 1.1 2009/03/06 01:13:01 mengqingyu Exp $ * Create: 2013-3-28 下午01:10:51 */ public class Main { public static void main(String[] args) throws InterruptedException { BlockingQueue<MyData> queue = new LinkedBlockingQueue<MyData>(10); Producer producer = new Producer(queue); Consumer consumer = new Consumer(queue); ExecutorService service = Executors.newCachedThreadPool(); service.execute(producer); service.execute(producer); service.execute(producer); service.execute(consumer); Thread.sleep(10000); producer.stop(); System.out.println("producer stop!"); Thread.sleep(10000); System.out.println("consumer stop!"); consumer.stop(); service.shutdown(); } } //下面是链式阻塞队列的内部简单实现 public interface BlockingQueue<T> { /** * * @function:插入元素 * @param e * @throws InterruptedException * @author: mengqingyu 2013-4-2 上午10:39:19 */ void put(T e) throws InterruptedException; /** * * @function:取出元素 * @return * @throws InterruptedException * @author: mengqingyu 2013-4-2 上午10:39:16 */ T take() throws InterruptedException; } /** * * 类功能描述:阻塞队列的内部实现 * * @author <a href="mailto:qingyu.meng21@gmail.com">mengqingyu </a> * @version $Id: codetemplates.xml,v 1.1 2009/03/06 01:13:01 mengqingyu Exp @param <T> $ * Create: 2013-4-2 上午11:04:29 */ public class LinkedBlockingQueue<T> implements BlockingQueue<T>{ //链表容量 private final int capacity; //计数器 private final AtomicInteger count = new AtomicInteger(0); //队列头引用 private transient Node<T> head; //队列尾引用 private transient Node<T> last; //锁 private Lock lock = new ReentrantLock(); //生产者条件锁 private Condition condition_producer = lock.newCondition(); //消费者条件锁 private Condition condition_consumer = lock.newCondition(); //链表内部类 static class Node<T> { volatile T item; //数据区 Node<T> next; //后继结点 Node(T x) { item = x; } } public LinkedBlockingQueue(int capacity) { if (capacity <= 0) throw new IllegalArgumentException(); this.capacity = capacity; last = head = new Node<T>(null); } @Override public void put(T t) throws InterruptedException { if (t == null) throw new NullPointerException(); lock.lock(); try { while (count.get() == capacity) //当容量满时候等待,用循环的目的是每次被唤醒都需要重新检查是否满足条件。 condition_producer.await(); insert(t);//插入元素 System.out.println(Thread.currentThread().getName()+" push:"+(capacity-1)+t); count.getAndIncrement();//计数器加1 condition_consumer.signalAll(); } catch (InterruptedException e) { condition_consumer.signalAll(); e.printStackTrace(); } finally { lock.unlock(); } } /** * * @function:插入元素 * @param x * @author: mengqingyu 2013-4-2 上午10:36:52 */ private void insert(T x) { last = last.next = new Node<T>(x); } @Override public T take() throws InterruptedException { T x = null; lock.lock(); try{ while (count.get() == 0) condition_consumer.await(); x = extract(); System.out.println(Thread.currentThread().getName()+" pop:"+capacity+x); count.getAndDecrement(); condition_producer.signalAll(); } catch (InterruptedException e) { condition_producer.signalAll(); e.printStackTrace(); } finally { lock.unlock(); } return x; } /** * * @function:取出元素 * @return * @author: mengqingyu 2013-4-2 上午10:36:31 */ private T extract() { Node<T> first = head.next; head = first; T x = first.item; first.item = null; return x; } }
2.future模式
从java 5开始,Java提供了Callable接口,该接口是Runnable接口的增强版,Callable接口提供了一个call()方法,可以作为线程执行体,但call()方法比run()方法的功能更强大。
call()方法可以有返回值,call()方法可以声明抛出异常。
import java.util.concurrent.Callable; /** * * 类功能描述:实现Callable接口重写call方法,支持多线程下抛出异常,主线程阻塞等待子线程的返回值。 * * @author <a href="mailto:qingyu.meng21@gmail.com">mengqingyu </a> * @version $Id: codetemplates.xml,v 1.1 2009/03/06 01:13:01 mengqingyu Exp $ * Create: 2013-4-2 上午11:06:47 */ public class DataService implements Callable<String> { private String param; private static final int SLEEP_TIME = 1000; public DataService(String param){ this.param = param; } @Override public String call() throws InterruptedException{ StringBuffer sb=new StringBuffer(); for (int i = 0; i < 5; i++) { sb.append(param); System.out.println("Chlid Thread wait 1 second..."); Thread.sleep(SLEEP_TIME); } return sb.toString(); } } import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.FutureTask; /** * * 类功能描述:jdk实现,可以直接使用 * * @author <a href="mailto:qingyu.meng21@gmail.com">mengqingyu </a> * @version $Id: codetemplates.xml,v 1.1 2009/03/06 01:13:01 mengqingyu Exp $ * Create: 2013-3-28 下午01:11:43 */ public class Main { public static void main(String[] args) { FutureTask<String> future = new FutureTask<String>(new DataService("Hello ")); ExecutorService executor = Executors.newFixedThreadPool(1); try { executor.execute(future); System.out.println("Main Thread wait 2 second..."); Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("future.get() before"); try { System.out.println("result:" + future.get());//线程阻塞,直到取到返回值 } catch (Exception e) { e.printStackTrace(); } executor.shutdown(); } } //下面是future的内部实现 public interface Callable<T> { T call() throws Exception;; } public interface Future<T> { T get()throws Exception; } /** * * 类功能描述:future内部实现,包含获取结果集和异常信息的方法 * * @author <a href="mailto:qingyu.meng21@gmail.com">mengqingyu </a> * @version $Id: codetemplates.xml,v 1.1 2009/03/06 01:13:01 mengqingyu Exp @param <T> $ * Create: 2013-4-2 上午11:08:52 */ public class FutureTask<T> implements Future<T>,Runnable { private T data; private Exception exception; private Callable<T> callable; private boolean isReady = false; public FutureTask(Callable<T> callable) { this.callable = callable; } public synchronized T get() throws Exception { while (!isReady) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } if (exception != null) throw exception; return data; } @Override public synchronized void run() { if (isReady) { return; } try { data = callable.call(); } catch (Exception e) { exception = e; } isReady = true; notifyAll(); } }
发表评论
-
基于Mybatis封装的增删改查实现通用自动化sql
2014-12-07 20:59 41231.基于map或javaBean的增删改查可实现不写dao接口 ... -
S2SH读取XML扩展点
2014-07-27 01:39 680由于struts2、hibernate通过默认配置文件不支持通 ... -
导出Word、Excel、PPT通用功能设计思路
2014-03-07 16:25 1803工作中经常 ... -
HttpClient抓取解析网站支持多种验证方式
2014-03-06 10:10 1399工作中遇到了抓取多个项目数据并且有多种验证方式包括Http标准 ... -
Tomcat架构分析及性能调优
2014-02-16 17:59 3186一.Tomcat容器层级结构 分为四个等级,由四个子容器组成 ... -
基于FLEX,MINA消息推送核心代码
2013-10-21 16:15 1683import java.util.List; impor ... -
(转载)拦截器与过滤器的区别
2013-05-10 15:36 991拦截器与过滤器的区别 : 1.拦截器是基于java的反射机制 ... -
(转载)Struts中默认的拦截器栈
2013-05-10 13:19 5035Struts中默认的拦截器栈 ... -
海量数据、高并发优化方案
2013-03-16 18:53 8465一.应用服务器负载均衡 1.链路负载均衡 通过DNS解析域 ... -
Lucene 详解
2013-03-01 11:10 14841.什么是全文检索 对于搜索,按被搜索的资源类 ... -
Spring JDBC批量操作
2013-01-20 02:51 271、使用JdbcTemplate进行批 ... -
集成JBPM3,实现流程流转
2012-11-29 16:48 2106集成JBPM到OA系统,并实现JbpmFacade接口 - ... -
Spring MVC 文件上传
2010-03-23 16:45 1124MultipartHttpServletRequest mul ... -
导出Excel(jxl)
2009-08-05 09:15 526Action代码: import java.io.IOExce ... -
Hibernate笔记(常用技术)
2009-07-21 17:13 2123一.对象关系映射基础 1.hibernate对象属性映射 ...
相关推荐
在《JAVA并发编程实践》中,这 些便利工具的创造者不仅解释了它们究竟如何工作、如何使用,还阐释了创造它们的原因,及其背后的设计模式。JAVA并发编程实践英文版很不错,运用了n多知识。 本书作者都是Java ...
《JAVA并发编程实践》随着多核处理器的普及,使用并发成为构建高性能应用程序...在《JAVA并发编程实践》中,这些便利工具的创造者不仅解释了它们究竟如何工作、如何使用,还阐释了创造它们的原因,及其背后的设计模式。
Java并发编程:设计原则与模式Java并发编程:设计原则与模式
Java并发编程:设计原则与模式(第二版).pdf
《JAVA并发编程实践》随着多核处理器的普及,使用并发成为构建高性能应用程序...在《JAVA并发编程实践》中,这些便利工具的创造者不仅解释了它们究竟如何工作、如何使用,还阐释了创造它们的原因,及其背后的设计模式。
Java并发编程设计原则和模式.pdf
Java并发编程:设计原则与模式(第二版)
4.2.1 Java监视器模式 4.2.2 示例:车辆追踪 4.3 线程安全性的委托 4.3.1 示例:基于委托的车辆追踪器 4.3.2 独立的状态变量 4.3.3 当委托失效时 4.3.4 发布底层的状态变量 4.3.5 示例:发布状态的车辆追踪...
java 并发变成设计原则与模式第二版 PDF版本,下载即看
Java并发编程设计原则与模式.pdf Doug Lea的大作
《JAVA并发编程实践》并不仅仅提供并发API的清单及其机制,还提供了设计原则、模式和思想模型,使我们能够更好地构建正确的、性能良好的并发程序。《JAVA并发编程实践》适合于具有一定Java编程经验的程序员、希望...
Java并发编程_设计原则和模式(CHM)
java并发编程:设计原则与模式.rar
《JAVA并发编程实践》随着多核处理器的普及,使用并发成为构建高性能应用程序...在《JAVA并发编程实践》中,这些便利工具的创造者不仅解释了它们究竟如何工作、如何使用,还阐释了创造它们的原因,及其背后的设计模式。
《JAVA并发编程实践》随着多核处理器的普及,使用并发成为构建高性能应用程序...在《JAVA并发编程实践》中,这些便利工具的创造者不仅解释了它们究竟如何工作、如何使用,还阐释了创造它们的原因,及其背后的设计模式。
Java并发编程-设计原则与模式 pdf格式
跟踪插装\并发错误模式的Java并发程序动态测试方法 跟踪插装\并发错误模式的Java并发程序动态测试方法 跟踪插装\并发错误模式的Java并发程序动态测试方法跟踪插装\并发错误模式的Java并发程序动态测试方法
该书通过对常见的并发场景进行分析,通过场景+实践的模式使得对并发的学习更加的容易上手,清晰易懂
Java并发编程:设计原则与模式(第二版)-3