使用wait(),notifyAll()函数造成线程死锁

qyluo qyluo 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
下面是小弟的一个数据处理类,运行1个小时左右,run()函数就会死锁,process仍然继续运行!各位老大帮忙看看,急。。。。。。。。。。。。。。。。。。。。。。。。
核心类为run(),process()
具体调用方式请看main()函数

package com.mvtech.vasomp.slv.communication;

import java.util.*;

import org.apache.log4j.*;
import com.mvtech.vasomp.slv.utils.StopWatch;
import com.mvtech.vasomp.slv.datadeal.ParseCPcontent;
import com.mvtech.vasomp.slv.datadeal.ParseCPfrequence;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import com.mvtech.vasomp.slv.datadeal.ParseCPstat;
import com.mvtech.vasomp.slv.datadeal.ParseFrontAlert;


public class XmlProcessor
implements Runnable {
//主要的数据缓存区
public static LinkedList xmlQueue = new LinkedList();
public static long indicator = 0;

//the capacity of buf is set to 100M
public static final long BUF_LIMIT = 500000000;
public static String notice = "";

//the number of xml file inserted into queue
static int inXmls = 0;

//the number of xml file removed from queue
static int outXmls = 0;

String name = "";

static Category log = Category.getInstance(XmlProcessor.class);

public XmlProcessor() {}

public XmlProcessor(String ThreadName) {
this.name = ThreadName;
}

public void initName() {
this.name = Thread.currentThread().getName();
}

/**
* 往xmlQueue数据缓存区存入数据
* 从XML二级缓冲区插入一个XML文件到队列末尾。
* put xml file content to the end of queue
* @param data String:xml filecontent
* @param ip String
* @param type String
*/
public void process(String data, String ip, String type) {
XmlEntity newXml = new XmlEntity();
newXml.setContent(data);
newXml.setFrontIP(ip);
newXml.setFrontType(type);

//if inesert wait operation is wake and the buf is full,
//keep waiting
synchronized (xmlQueue) {
for (; ; ) {
if (this.indicator > BUF_LIMIT ||
this.indicator + data.length() > BUF_LIMIT) {
try {
xmlQueue.wait();
}
catch (InterruptedException ie) {
log.warn(name + " " + ip + ":" + ie.getMessage());
}
}
else {
//add new xml to the end of queue
xmlQueue.add(newXml);
//newXml
this.indicator += newXml.length();
inXmls++;
//log.info("nitify xml reader!");
xmlQueue.notifyAll();

break;
}
}
}

log.info("新xml文件进入队列,已经插入的文件个数为:" + inXmls +
",当前xml文件队列大小:" + (float)this.indicator / 1000000 + "M");

}

/**
* 解析处理xmlQueue数据缓存区的数据
* 从XML文件处理二级缓冲区取出一个XML文件,并
* 交由XML文件解析器处理。
*/
public void run() {
for (; ; ) {
XmlEntity xml = null;
synchronized (xmlQueue) {
for (; ; ) {
try {
if (indicator <= 0) {
// wait for data writer
xmlQueue.wait();
}
else {
xml = (XmlEntity) xmlQueue.removeFirst();
this.indicator -= xml.length();
outXmls++;
xmlQueue.notifyAll();
break;
}
}
catch (InterruptedException ex) {
}
}
}
if (xml == null) {
continue;
}
StopWatch watch = new StopWatch(true);
xml.persist();
watch.stop();
log.info("线程:" + name + ":" + "解析xml文件开销时间:" +
watch);

}
}

public static void main(String[] args) {

for (int i = 0; i < 30; i++) {
XmlProcessor xp = new XmlProcessor("Thread" + i);
Thread t = new Thread(xp);
t.setPriority(Thread.MAX_PRIORITY);
t.setName("Thread" + i);

t.start();
}

byte[] b = null;
try {
FileInputStream fi = new FileInputStream(
"D:\\My Documents\\put_dataCompressed");
int totalLen = fi.available();
log.info("totalLen=" + totalLen);
b = new byte[totalLen];
fi.read(b);
}
catch (Exception ex) {
ex.printStackTrace();
}

if (b == null) {
return;
}

for (; ; ) {
StopWatch watch = new StopWatch(true);
XmlProcessor xp = XmlProcessorFactory.getXmlProcess(
"192.168.0.1");
xp.process(new String(b), "192.168.0.1", "cp");
watch.stop();
log.info("The time of process a xml " +
watch);

try {
Thread.currentThread().sleep(1000 * 3);
}
catch (Exception e) {
e.printStackTrace();
}
}
}

}
...全文

相关推荐

libvpx-utils-1.3.0-8.el7.x64-86.rpm.tar.gz

1、文件内容:libvpx-utils-1.3.0-8.el7.rpm以及相关依赖 2、文件形式:tar.gz压缩包 3、安装指令: #Step1、解压 tar -zxvf /mnt/data/output/libvpx-utils-1.3.0-8.el7.tar.gz #Step2、进入解压后的目录,执行安装 sudo rpm -ivh *.rpm 4、安装指导:私信博主,全程指导安装

比特魔法师

HCIA-Datacom 22. 园区网典型组网架构及案例实践

大型园区网络可能是覆盖多幢建筑的网络,也可能是通过WAN连接一个城市内的多个园区的网络。当前网段并没有引入冗余链路,为防止办公人员误操作造成的二层网络环路呢,可在二层网络采用生成树技术,防止环路产生。同时建议手工配置Agg-S1为根桥。中型网络引入了按功能进行分区的理念,也就是模块化的设计思路,但功能模块相对较少。端口级别的可靠性:为了增加接入交换机与汇聚交换机之间的可靠性,同时为了增加链路带宽,采用以太网链路聚合技术。根据人员结构划分,分为访客VLAN,研发部VLAN,市场部VLAN,行政部VLAN。

风褛_襟花

广东屈臣氏饮料制造项目-全专业-20240508.nwd

广东屈臣氏饮料制造项目_全专业-20240508.nwd

meslog

Python3 if多条件简写技巧

Python3支持极简if条件写法,简化冗余代码。链式比较:原生支持1<x<10,无需写成x>1 and x<10,可读性更强。三元表达式:适用于二分支逻辑,语法 结果A if 条件 else 结果B,适合单行赋值,禁止多层嵌套三元表达式,会大幅降低可读性。逻辑短路:and左侧为False直接终止判断,or左侧为True直接终止判断,可用于空值兜底,比如name = input_name or "匿名用户"。注意不要混用=和==,新手常误将赋值写在if判断内,Python3会直接抛出语法异常。 6thhosp-east.com 3wfuwu.com ajfcw.net www.ahdmhbkj.cn www.aiyouseo.com

2601_97221235

PHP 代码加密工具 源码保护与站点授权

把 PHP 源码加密成不可直接阅读、难以还原的形式:变量名与控制流被打散重写,载荷带完整性自校验,改一个字节就停止运行。提供编码型 / 混淆型 / 重构型 / 底层型 / 虚拟机型五档保护强度,并可叠加按域名与期限的网站注册授权。

穿透云
评论 12
收藏 已收藏
分享
返回顶部
qyluo
Java SE 62,620 成员 307,251 内容