8个同级的文件夹,每个文件夹下有10个*.sh文件和10个子文件夹。这样的话,每个文件夹我都需要10个线程来做监控; 逻辑就是,当10个子文件夹中的文件数=我要求的数目(比如说25个或者50个),我就调用*.sh文件对其装载.而且这种调用都在半夜0点到8点之间,其它时间所有线程都是停止或者休眠的状态。总共要80个线程,怎么管理?

解决方案 »

  1.   

    stop()不能用了,改用interrupt(),可以?
      

  2.   

    我觉得两个子线程就可以了,一个线程负责轮循,如果满足就设个标志,让另个线程进行 shell的调用,
      

  3.   

    import java.io.File;
    import java.util.Calendar;public class ConquerFinder extends Thread {

            // 文件夹路径
    private String folderPath = null;

            // 达到多少文件
    private int limitCount = 0;

            // 是否正在执行
    private boolean isExecute = false;

    public ConquerFinder(String folderPath, int limitCount) {
    this.folderPath = folderPath;
    this.limitCount = limitCount;
    }

    public void run() {

    while(true) {
                            // 得到当前时
    int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
    if (hour > 0 && hour < 8) {
    isExecute = false;
    continue;
    }
    if (isExecute) {
    continue;
    }

    File folder = new File(folderPath);
                            // 如果不是文件夹路径,推出
    if (!folder.isDirectory()) {
    System.err.print("File path is not a directory");
    System.exit(0);
    }

                            // 得到文件夹下子文件的数量
    File[] sonFile = folder.listFiles();
    if (sonFile.length > limitCount) {
    // 如果大于所要数量,此处执行.sh
                                    // 设置执行flag
    isExecute = true;
                                    
    }
    } }
    }
    我这里只能提供你一个思路,这个线程可以看成是一个死循环,并且在判断时间的时候可以设置如果没有在这个时间范围内,sleep一段时间,看你需求了。
      

  4.   

    上面的只是一个思路,你也可以把你子文件夹的路径,和父文件夹的路径一起传进来。也可以把sh得路径传进来。也可以在File[] sonFile = folder.listFiles();这部用循环扫描,看你兴趣了。
      

  5.   

    线程写好了,从判断到调用shell文件但是问题又来了,很奇怪there is 13 files, then load......../db2home/db2inst1/ap/ap_load_sx.sh
    read line:proess is running!
    check dir_ap:/db2home/db2inst1/ap/ap_file_he/
    there is 13 files, then load......../db2home/db2inst1/ap/ap_load_he.sh
    read line:proess is running!这里判断是说,2个shell文件都在跑着,但实际上没有跑
    这是单独运行ap_load_sx.sh和ap_load_he.sh 都可以偏偏我的线程调用shell时,shell自己打印proess is running!奇怪!
      

  6.   

    线程写好了,从判断到调用shell文件但是问题又来了,很奇怪there is 13 files, then load......../db2home/db2inst1/ap/ap_load_sx.sh
    read line:proess is running!
    check dir_ap:/db2home/db2inst1/ap/ap_file_he/
    there is 13 files, then load......../db2home/db2inst1/ap/ap_load_he.sh
    read line:proess is running!这里判断是说,2个shell文件都在跑着,但实际上没有跑
    这是单独运行ap_load_sx.sh和ap_load_he.sh 都可以偏偏我的线程调用shell时,shell自己打印proess is running!奇怪!
      

  7.   

    发现问题所在了,但是不知道怎么解决
    Process pro = Runtime.getRuntime().exec("ap_load_he.sh"); 这样是可以的
    Process pro = Runtime.getRuntime().exec("/db2home/db2inst1/ap/ap_load_he.sh"); 
    这样就不行,报proess is running! 
      

  8.   

    “proess is running  ” 是shell程序打印的,我读了出来
      

  9.   

    估计是shell只支持当前目录执行String cmd[] = {"sh","/bin/bash","cd /db2home/db2inst1/ap/;pwd; ap_load_he.sh"};
    Process pro = Runtime.getRuntime().exec(cmd); 这样好像也不行