同步阻塞
客户端提交Event请求到服务器,服务器收到请求后开启线程处理,处理完后再将结果集返回给客户端。
- 同步Event提交,客户端等待时间长(提交时长+创建Thread时长+业务处理时长+返回结果时长)会陷入阻塞,导致二次提交Event耗时过长。
- 限制系统同时受理业务数量,整体吞吐量不高。
- 频繁创建销毁线程,增加系统额外开销。
- 大量业务处理线程阻塞会导致CPU上下文切换频繁,降低系统性能。
异步非阻塞
客户端提交Event请求到服务器后会得到一个工单并且立即返回,Event则会被放置在队列中,服务器的多个线程不断从队列中取任务进行异步处理,最后结果都保存在结果集中,客户端可通过回调函数凭工单查询结果。
1.客户端不用等到结果 处理完才返回,提高了系统吞吐量。
2.线程可重复利用,减少创建和销毁线程的资源浪费。
3.线程数量在一定范围内不会导致CPU上下文切换频繁。
单线程间的通信
创建一个eventQueue 队列,一个线程添加队列,队列满时就调用wait(),一个线程取出队列中得Event,队列为空时就调用wait().
import java.util.LinkedList;
public class SingleCommun {
private final int max;
private final LinkedList<Event> eventQueue = new LinkedList<Event>();
private final static int DEFAULT_MAX_EVENT = 10;
static class Event{
}
public SingleCommun() {
this(DEFAULT_MAX_EVENT);
}
public SingleCommun(int max) {
this.max = max;
}
public void off(Event event) {
synchronized (eventQueue) {
if(eventQueue.size()>=max) {
console("the queue is full");
try {
eventQueue.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
console("the new event is submitted"+eventQueue.size());
eventQueue.addLast(event);
eventQueue.notify();
}
}
public Event task() {
synchronized (eventQueue) {
if(eventQueue.isEmpty()) {
console("the queue is empty");
try {
eventQueue.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Event event = eventQueue.removeFirst();
this.eventQueue.notify();
console("the event "+ event+"is handled"+eventQueue.size());
return event;
}
}
private void console(String msg) {
System.out.println(Thread.currentThread().getName()+" "+msg);
}
}
//测试代码
public static void main(String[] args) {
final SingleCommun eventQueue = new SingleCommun();
new Thread(()->{
for(;;) {
eventQueue.off(new SingleCommun.Event());
}
},"生产者线程").start() ;
new Thread(()->{
for(;;) {
eventQueue.task();
try {
TimeUnit.MILLISECONDS.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
},"消费者线程").start() ;
}
输出
生产者线程 the new event is submitted0
生产者线程 the new event is submitted1
生产者线程 the new event is submitted2
生产者线程 the new event is submitted3
生产者线程 the new event is submitted4
生产者线程 the new event is submitted5
生产者线程 the new event is submitted6
生产者线程 the new event is submitted7
生产者线程 the new event is submitted8
生产者线程 the new event is submitted9
生产者线程 the queue is full
消费者线程 the event communication.SingleCommunEvent@626afd7fishandled9生产者线程theneweventissubmitted9生产者线程thequeueisfull消费者线程theeventcommunication.SingleCommunEvent@626afd7fis handled9
生产者线程 the new event is submitted9
生产者线程 the queue is full
消费者线程 the event communication.SingleCommunEvent@626afd7fishandled9生产者线程theneweventissubmitted9生产者线程thequeueisfull消费者线程theeventcommunication.SingleCommunEvent@6cad1f42is handled9
生产者线程 the new event is submitted9
生产者线程 the queue is full
消费者线程 the event communication.SingleCommun$Event@6892960fis handled9
生产者线程 the new event is submitted9
生产者线程 the queue is full
根据输出日志可以看出,在生产者线程把队列添加满时,线程wait()然后释放eventQueue的monitor所有权,消费者线程获取到eventQueue的monitor所有权,取出队列中得第一个Event,调用notify()唤醒了因调用wait()而放弃eventQueue的monitor所有权进入 wait set中得生产者线程,生产者线程再次提交Event,如此反复执行。
关于wait和notify
- wait和notify时Object中的方法,不是Thread中特有的。
- wait()线程进入阻塞,直到其他线程将其唤醒否则用于处于阻塞状态。
- wait(long timeout) timeout的单位为毫秒,其他线程将其唤醒或者到达指定时间自动唤醒。
- wait(long timeout, int nanos) nanos的单位为纳秒, 1毫秒 = 1000 微秒 = 1000 000 纳秒
- 调用wait的线程必须拥有对象的monitor,就是说方法必须在同步方法或者代码块中使用。
- 线程调用wait后将会放弃该对象的monitor所有权并加入到与之关联的wait set中。
- noitfy唤醒单个正在执行该对象wait方法的线程,没有则忽略。jvm的wait set 唤醒规则是先进先出的方式弹出。
- 被唤醒的线程需要重新获得对象关联的monitor的lock才能继续执行。
public final void wait() throws InterruptedException {
wait(0);
}
public final native void wait(long timeout) throws InterruptedException;
public final void wait(long timeout, int nanos) throws InterruptedException {
if (timeout < 0) {
throw new IllegalArgumentException("timeout value is negative");
}
if (nanos < 0 || nanos > 999999) {
throw new IllegalArgumentException(
"nanosecond timeout value out of range");
}
if (nanos > 0) {
timeout++;
}
wait(timeout);
}
从wait源码可以看出wait(0)代表永不超时,wait(long timeout, int nanos)方法中 nanos在0到999999之间时就会timeout加1毫秒。
wait和sleep对比
- wait和sleep方法都可以使线程进入阻塞状态。
- wait和sleep方法都是可中断方法,被中断后都会收到中断异常。
- wait时Object方法,sleep时Thread特有方法。
- wait必须在同步方法或者代码块中执行,sleep不需要。
- 在线程同步方法中执行wait时会释放monitor锁,而sleep不会释放。
- sleep方法断暂休眠后会主动退出阻塞,wait在没有指定时间的情况下是需要被其他线程唤醒或者中断才能退出阻塞。
2730

被折叠的 条评论
为什么被折叠?



