我在python中有一个半工作脚本。我正在尝试在最新的线程转储文件中搜索单词列表。我想搜索RUNNABLE和WAITING,希望这些词更常见,并且可能有重复(为避免Runnable和重复的词重复,我想搜索完整路径以查找唯一性,例如java.lang.Thread.State:WAITING )。任何输入都会有所帮助,谢谢您。
import os
def wordcount(filename, listwords):
try:
file = open(filename, encoding ='ISO-8859-1')
read = file.readlines()
file.close()
for word in listwords:
#lower = word.lower()
count = 0
for sentence in read:
line = sentence.split()
for each in line:
line2 = each.upper()
#line2 = line2.strip("java.lang.Thread.State: ")
if word == line2:
count += 1
print (word, ":", count)
except FileExistsError:
print ("Thread dump is not there")
path = '/Users/YEscobar/Desktop/vidjet/pub1a'
filePath = [os.path.join(path, fname) for fname in os.listdir(path)]
lastFile = sorted(filePath, key=os.path.getctime)[-1]
wordcount (lastFile,["java.lang.Thread.State: RUNNABLE","BLOCKED", "TIMED_WAITING", "java.lang.Thread.State: WAITING"])
控制台
/Users/YEscobar/Desktop/videojet/pub1a/jstack.20180806-115501.log
java.lang.Thread.State: RUNNABLE : 0
BLOCKED : 14
TIMED_WAITING : 27
java.lang.Thread.State: WAITING : 0
线程转储文件示例:
"Thread-3857" daemon prio=10 tid=0x0000000041755800 nid=0x28ad in
Object.wait() [0x00007f7ca9c5e000]
java.lang.Thread.State: TIMED_WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x0000000702755d60> (a java.lang.Object)
at EDU.oswego.cs.dl.util.concurrent.LinkedQueue.poll(Unknown Source)
- locked <0x0000000702755d60> (a java.lang.Object)
at EDU.oswego.cs.dl.util.concurrent.PooledExecutor.getTask(Unknown Source)
at EDU.oswego.cs.dl.util.concurrent.PooledExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Thread.java:662)
"RMI TCP Connection(1492)-10.35.191.18" daemon prio=10
tid=0x00007f7cd8022000 nid=0x28a0 runnable [0x00007f7ca9e60000]
java.lang.Thread.State: RUNNABLE
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:129)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
at java.io.BufferedInputStream.read(BufferedInputStream.java:237)
- locked <0x000000079a604a30> (a java.io.BufferedInputStream)
at java.io.FilterInputStream.read(FilterInputStream.java:66)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:517)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
at java.lang.Thread.run(Thread.java:662)
"10.170.13.146 [1533556368499] <closed>" daemon prio=10
tid=0x00007f7cfc086000 nid=0xab8 in Object.wait() [0x00007f7caa062000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x0000000712e22948> (a
com.day.j2ee.servletengine.HttpListener$Worker)
at java.lang.Object.wait(Object.java:485)
at com.day.j2ee.servletengine.HttpListener$Worker.await(HttpListener.java:606)
- locked <0x0000000712e22948> (a com.day.j2ee.servletengine.HttpListener$Worker)
at com.day.j2ee.servletengine.HttpListener$Worker.run(HttpListener.java:631)
at java.lang.Thread.run(Thread.java:662)