首页 > 编程知识 正文

springboot启动一个线程,java线程池实际项目案例

时间:2023-05-05 23:12:55 阅读:39995 作者:332

这几天,同事告诉物联网设备,前端请求数据,后端接口发起TCP请求,另一个线程接收数据,然后必须等待TCP接收完成才能返回前端。 此时,需要屏蔽来自前端的请求,在TCP接收到数据后,向前端返回数据。 所以我写了道具文件

import Java.util.concurrent.concurrent hashmap; import Java.util.concurrent.locks.lock support; 包含public class LockSupportUtil { //线程引用的全局容器publicstaticfinalconcurrenthashmapinteger, thread thread _ pointer _ map=newconcurrrer/* * *锁定线程* * @param address存储线程的密钥,并使用system.identity hashcode (ttharam @param thread线程对象(* @param millisecond超时时线程* @return */)/publicstaticvoidlock (int address,Thread thread ) long millisecond ) /如果地址已占用,则直接释放thread thread 001=thread _ pointer _ m monter (thread 001!=null } { lock support.un park (thread 001 ); thread _ pointer _ map.remove (地址; } thread _ pointer _ map.put (地址,thread ); locksupport.parkuntil(thread,System.currentTimeMillis ) (millisecond ); } /** *释放线程* * @ param地址线程密钥y */publicstaticvoidunlock (int address ) ) thread thread=thread _ pointer _ map=null } { lock support.un park (thread ); thread _ pointer _ map.remove (地址; }}使用:携带提交TCP请求时生成的线程密钥,即address。 TCP异步返回数据时带上这个密钥。 然后用这个Key释放线程。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。