我编写了java代码来删除特定路径中的文件。我们可以使用以下命令查看进程,我们只找到一个条目。
root@ad4d # ps -ef | grep Del
root 27896 27895 0 Jun 24 pts/5 0:49 java -jar /oracle/php/online/jars/dist/DelFile.jar
但是当管理员检查从下面的命令运行的进程时,我们可以看到下面的许多子线程和父线程进程
root@almt4d # ps -aAdeflcjLPyZ | grep Del
S global root 27896 27895 26891 26891 1 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 2 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 3 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 4 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 5 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 6 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 7 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 8 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 9 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 10 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 11 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 12 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 13 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 14 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 15 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 16 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 17 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 18 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 19 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 20 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 21 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 22 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 23 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 24 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 25 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 26 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 27 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 28 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 29 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 30 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 31 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 32 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 33 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 34 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 35 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 36 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 37 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 38 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 39 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 40 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 41 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 42 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 43 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 44 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 45 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 46 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 47 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 48 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 49 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 50 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 51 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 52 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 53 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 54 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 55 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 56 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 57 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 58 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 59 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 60 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 61 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 62 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 63 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 64 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 65 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 66 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 67 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 68 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 69 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 70 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 71 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 72 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 73 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 74 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 75 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 76 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 77 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 78 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 79 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 80 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 81 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 82 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 83 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 84 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 85 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 86 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 87 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 88 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:04 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 89 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 90 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 91 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 92 - 96 TS 58 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 93 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 94 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 95 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:45 java -jar /oracle/php/online/jars/d
S global root 27896 27895 26891 26891 96 - 96 TS 59 37144 116392 ? Jun 24 pts/5 0:00 java -jar /oracle/php/online/jars/d
任何人都可以解释这些子线程和父线程进程开始执行的位置。 我只执行了一个只有一个线程的DelFile.java 无论如何,使用下面的命令
来控制管理员可见的子线程root@almt4d # ps -aAdeflcjLPyZ | grep Del
DelFile.java
package DelFile;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.SimpleLayout;
public class DelFile extends Thread{
static Logger log =Logger.getLogger(DelFile.class);
FileAppender app = null;
public DelFile()throws IOException{
app = new FileAppender(new SimpleLayout(), "/oracle/php/online/jars/dist/del_file.log");
}
public void logSet(){
app.setLayout(new PatternLayout(("%d{yyyy-MM-dd HH:mm:ss} %-5.5p [%t] %m%n")));
log.addAppender(app);
}
boolean flage=true;
public void run(){
while(flage){
final String str="xls";
String add="/oracle/php/online/jars/dist/";
String[] paths;
File f = null;
try{
f = new File(add);
paths=f.list();
for(String path:paths)
{
String temp="";
String temp1="";
int count=0;
for(int i=path.length()-1;i>=0;i--){
if(path.charAt(i)!='.'){
count++;
temp+=path.charAt(i);//pmbs
}else{
for(int j=temp.length()-1;j>=0;j--){
temp1+=temp.charAt(j);
}
if(str.length()==count){
break;
}
}
}
if(str.equals(temp1)){
long time= new File(add+path).lastModified();
long utime=time+600000l;
while(utime>System.currentTimeMillis()){
Thread.sleep(601000);
}
logSet();
log.info(path +" : File is deleted");
new File(add+path).delete();
}
}
}catch(NullPointerException np){
try{
Thread.sleep(600000);
}catch(InterruptedException ip) {}
}catch(InterruptedException ie){
}
}
}
public static void main(String[] args)throws IOException{
DelFile r=new DelFile();
r.start();
}
}
答案 0 :(得分:1)
boolean flage=true;
public void run(){
while(flage){
// flage not referenced below, no break, no return
...
}
这称为无限循环。它导致线程永远运行,这解释了ps看到的许多条目。
<强>后来强>
您看到的LWP(或线程)是JVM内部线程 - 而不是您的程序创建的线程。我以为你担心为什么他们会留在机器里,显然是永远的,这确实是由于无限循环:即使所有文件都消失,程序也不会终止。