线程间的通信(一)

同步阻塞
客户端提交Event请求到服务器,服务器收到请求后开启线程处理,处理完后再将结果集返回给客户端。

  1. 同步Event提交,客户端等待时间长(提交时长+创建Thread时长+业务处理时长+返回结果时长)会陷入阻塞,导致二次提交Event耗时过长。
  2. 限制系统同时受理业务数量,整体吞吐量不高。
  3. 频繁创建销毁线程,增加系统额外开销。
  4. 大量业务处理线程阻塞会导致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

  1. wait和notify时Object中的方法,不是Thread中特有的。
  2. wait()线程进入阻塞,直到其他线程将其唤醒否则用于处于阻塞状态。
  3. wait(long timeout) timeout的单位为毫秒,其他线程将其唤醒或者到达指定时间自动唤醒。
  4. wait(long timeout, int nanos) nanos的单位为纳秒, 1毫秒 = 1000 微秒 = 1000 000 纳秒
  5. 调用wait的线程必须拥有对象的monitor,就是说方法必须在同步方法或者代码块中使用。
  6. 线程调用wait后将会放弃该对象的monitor所有权并加入到与之关联的wait set中。
  7. noitfy唤醒单个正在执行该对象wait方法的线程,没有则忽略。jvm的wait set 唤醒规则是先进先出的方式弹出。
  8. 被唤醒的线程需要重新获得对象关联的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对比

  1. wait和sleep方法都可以使线程进入阻塞状态。
  2. wait和sleep方法都是可中断方法,被中断后都会收到中断异常。
  3. wait时Object方法,sleep时Thread特有方法。
  4. wait必须在同步方法或者代码块中执行,sleep不需要。
  5. 在线程同步方法中执行wait时会释放monitor锁,而sleep不会释放。
  6. sleep方法断暂休眠后会主动退出阻塞,wait在没有指定时间的情况下是需要被其他线程唤醒或者中断才能退出阻塞。
(Kriging_NSGA2)克里金模型结合多目标遗传算法求最优因变量及对应的最佳自变量组合研究(Matlab代码实现)内容概要:本文介绍了克里金模型(Kriging)与多目标遗传算法NSGA-II相结合的方法,用于求解最优因变量及其对应的最佳自变量组合,并提供了完整的Matlab代码实现。该方法首先利用克里金模型构建高精度的代理模型,逼近复杂的非线性系统响应,减少计算成本;随后结合NSGA-II算法进行多目标优化,搜索帕累托前沿解集,从而获得多个最优折衷方案。文中详细阐述了代理模型构建、算法集成流程及参数设置,适用于工程设计、参数反演等复杂优化问题。此外,文档还展示了该方法在SCI区论文中的复现应用,体现了其科学性与实用性。; 适合人群:具备定Matlab编程基础,熟悉优化算法和数值建模的研究生、科研人员及工程技术人员,尤其适合从事仿真优化、实验设计、代理模型研究的相关领域工作者。; 使用场景及目标:①解决高计算成本的多目标优化问题,通过代理模型降低仿真次数;②在无法解析求导或函数高度非线性的情况下寻找最优变量组合;③复现SCI高水平论文中的优化方法,提升科研可信度与效率;④应用于工程设计、能源系统调度、智能制造等需参数优化的实际场景。; 阅读建议:建议读者结合提供的Matlab代码逐段理解算法实现过程,重点关注克里金模型的构建步骤与NSGA-II的集成方式,建议自行调整测试函数或实际案例验证算法性能,并配合YALMIP等工具包扩展优化求解能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值