线程间的通信(一)

同步阻塞
客户端提交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在没有指定时间的情况下是需要被其他线程唤醒或者中断才能退出阻塞。
通过短时倒谱(Cepstrogram)计算进行时-倒频分析研究(Matlab代码实现)内容概要:本文主要介绍了项关于短时倒谱(Cepstrogram)计算在时-倒频分析中的研究,并提供了相应的Matlab代码实现。通过短时倒谱分析方法,能够有效提取信号在时间与倒频率域的特征,适用于语音、机械振动、生物医学等领域的信号处理与故障诊断。文中阐述了倒谱分析的基本原理、短时倒谱的计算流程及其在实际工程中的应用价值,展示了如何利用Matlab进行时-倒频图的可视化与分析,帮助研究人员深入理解非平稳信号的周期性成分与谐波结构。; 适合人群:具备定信号处理基础,熟悉Matlab编程,从事电子信息、机械工程、生物医学或通信等相关领域科研工作的研究生、工程师及科研人员。; 使用场景及目标:①掌握倒谱分析与短时倒谱的基本理论及其与傅里叶变换的关系;②学习如何用Matlab实现Cepstrogram并应用于实际信号的周期性特征提取与故障诊断;③为语音识别、机械设备状态监测、振动信号分析等研究提供技术支持与方法参考; 阅读建议:建议读者结合提供的Matlab代码进行实践操作,先理解倒谱的基本概念再逐步实现短时倒谱分析,注意参数设置如窗长、重叠率等对结果的影响,同时可将该方法与其他时频分析方法(如STFT、小波变换)进行对比,以提升对信号特征的理解能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值