我在做一个java调用c的dll的一个接口程序,需要每分钟读取dll文件中的数据,读取记录的方法已经有,可以读出来。但是现在的需求是需要每分钟去读取dll文件中新增的记录。尝试用线程,但是一直报错说Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread
at java.lang.Thread.start0(Native Method)
at java.lang.Thread.start(Unknown Source)
at test.zkem.Main.main(Main.java:63)
也就是不能创建native的线程。大家有一个好的方法可以实现每分钟执行native方法的方法吗?请赐教

解决方案 »

  1.   

    我把自己的程序贴出来大家帮忙看看public class Main {
        static IZKEM czkem = ClassFactory.createCZKEM();
        private static int count=0;    public  void readUserInfo(){//读取用户信息的方法
            Holder<String> name = new Holder<String>();
            Holder< String> dwEnrollNumber=new Holder<String>();
            Holder<String> password = new Holder<String>();
            Holder<Integer> privilege = new Holder<Integer>();
            Holder<Boolean> enabled = new Holder<Boolean>();
            czkem.readAllUserID(1);
            while(czkem.ssR_GetAllUserInfo(1, dwEnrollNumber, name,
                    password, privilege, enabled)==true){
                System.out.println("**********");
                System.out.println(name.value+" "+password.value+" "
                        +privilege.value+" "+enabled.value+" "+czkem.cardNumber(0));
            } 
            
        }
        
        public  void readNewRecord(){//每次更新只读取新记录的方法
            Holder< String> dwEnrollNumber=new Holder<String>();
            Holder<Integer> dwVerifyMode = new Holder<Integer>();
            Holder<Integer> dwInOutMode = new Holder<Integer>();
            Holder<Integer> dwYear = new Holder<Integer>();
            Holder<Integer> dwMonth = new Holder<Integer>();
            Holder<Integer> dwDay = new Holder<Integer>();
            Holder<Integer> dwHour = new Holder<Integer>();
            Holder<Integer> dwMinute = new Holder<Integer>();
            Holder<Integer> dwSecond = new Holder<Integer>();
            Holder<Integer> dwWorkCode = new Holder<Integer>();
            czkem.setLastCount(count);//从count开始读取新记录。
            while(czkem.ssR_GetGeneralLogData(1, dwEnrollNumber,
                    dwVerifyMode, dwInOutMode, dwYear, dwMonth,
                    dwDay, dwHour, dwMinute, dwSecond, dwWorkCode)==true){
                count++;
                System.out.println(dwVerifyMode.value+" "+dwInOutMode.value+" "
                        +dwYear.value+" "+dwMonth.value+" "+dwDay.value+" "+dwHour.value+" "
                        +dwMinute.value+" "+dwSecond.value+dwWorkCode.value);
            }
            System.out.println("count: "+count);
        }
        
    //    public void run(){//run方法
    ////        readNewRecord();
    //        try {
    //            Thread.sleep(1000*3);
    //        } catch (InterruptedException e) {
    //            // TODO Auto-generated catch block
    //            e.printStackTrace();
    //        }
    //    }    public static void main(String[] args){
            if (czkem.connect_Net("192.168.1.201", 4370)) {
                new Main().readUserInfo();//调用方法读取所有用户信息
                System.out.println("read the record now *******"); 
                new Main().readNewRecord();//调用方法读取新记录
    //                while(true){
    //                    Main m=new Main();
    ////                    m.start();
    //                }
               }
         }
        
    }
    运行结果:
    **********
    Gary Mak  0 true 1663032
    **********
    amber 123456 0 true 11344672
    **********
    飘叶 0 true 1645088read the record now *******1 -1 2010 2 23 16 1 00
    2 -1 2010 2 23 17 8 420
    2 -1 2010 2 23 17 16 390
    2 -1 2010 2 23 17 16 520
    1 -1 2010 2 24 14 57 280
    2 -1 2010 2 24 14 57 420
    1 -1 2010 2 24 15 39 110
    2 -1 2010 2 24 15 39 390
    2 -1 2010 2 24 15 39 580
    2 -1 2010 2 24 15 40 30
    2 -1 2010 2 24 15 40 50
    2 -1 2010 2 24 15 40 70
    2 -1 2010 2 24 15 40 100
    2 -1 2010 2 24 15 40 130
    2 -1 2010 2 24 15 40 150
    1 -1 2010 2 24 18 50 80
    2 -1 2010 2 24 18 50 210
    1 -1 2010 2 25 8 46 80
    count: 18但是当我把注释的放开,此方法直接继承Thread时,调用就会出现问题:**********
    Gary Mak  0 true 1663032
    **********
    amber 123456 0 true 11344672
    **********
    飘叶 0 true 1645088
    read the record now *******
    1 -1 2010 2 23 16 1 00
    2 -1 2010 2 23 17 8 420
    2 -1 2010 2 23 17 16 390
    2 -1 2010 2 23 17 16 520
    1 -1 2010 2 24 14 57 280
    2 -1 2010 2 24 14 57 420
    1 -1 2010 2 24 15 39 110
    2 -1 2010 2 24 15 39 390
    2 -1 2010 2 24 15 39 580
    2 -1 2010 2 24 15 40 30
    2 -1 2010 2 24 15 40 50
    2 -1 2010 2 24 15 40 70
    2 -1 2010 2 24 15 40 100
    2 -1 2010 2 24 15 40 130
    2 -1 2010 2 24 15 40 150
    1 -1 2010 2 24 18 50 80
    2 -1 2010 2 24 18 50 210
    1 -1 2010 2 25 8 46 80
    count: 18
    count: 18
    count: 18
    Exception in thread "Thread-26" count: 18
    count: 18
    com4j.ExecutionException: java.lang.NullPointerException
    at com4j.ComThread.execute(ComThread.java:189)
    at com4j.Task.execute(Task.java:32)
    at com4j.Wrapper$InvocationThunk.invoke(Wrapper.java:248)
    at com4j.Wrapper.invoke(Wrapper.java:119)
    at $Proxy5.setLastCount(Unknown Source)
    at test.zkem.Main.readNewRecord(Main.java:36)
    at test.zkem.Main.run(Main.java:49)
    Caused by: java.lang.NullPointerException
    at com4j.Wrapper$InvocationThunk.call(Wrapper.java:258)
    at com4j.Task.invoke(Task.java:44)count: 18 at com4j.ComThread.run0(ComThread.java:149)
    at com4j.ComThread.run(ComThread.java:125)
    Exception in thread "Thread-21" java.lang.NullPointerException
    at $Proxy5.setLastCount(Unknown Source)
    at test.zkem.Main.readNewRecord(Main.java:36)
    at test.zkem.Main.run(Main.java:49)
    count: 18
    Exception in thread "Thread-90" java.lang.NullPointerException
    at $Proxy5.setLastCount(Unknown Source)
    at test.zkem.Main.readNewRecord(Main.java:36)
    at test.zkem.Main.run(Main.java:49)
    Exception in thread "Thread-92" com4j.ExecutionException: java.lang.NullPointerException
    at com4j.ComThread.execute(ComThread.java:189)
    at com4j.Task.execute(Task.java:32)
    at com4j.Wrapper$InvocationThunk.invoke(Wrapper.java:248)
    at com4j.Wrapper.invoke(Wrapper.java:119)
    at $Proxy5.setLastCount(Unknown Source)
    at test.zkem.Main.readNewRecord(Main.java:36)
    at test.zkem.Main.run(Main.java:49)
    count: 18
    Caused by: java.lang.NullPointerException
    at com4j.Wrapper$InvocationThunk.call(Wrapper.java:258)
    at com4j.Task.invoke(Task.java:44)
    at com4j.ComThread.run0(ComThread.java:149)
    at com4j.ComThread.run(ComThread.java:125)
    count: 18
    Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread
    at java.lang.Thread.start0(Native Method)
    at java.lang.Thread.start(Unknown Source)
    at test.zkem.Main.main(Main.java:65)我还有一个问题是我的程序里面明明只有两个线程,为何在出错的时候竟然有Thread-90之类的线程,是有很多线程吗?
      

  2.   

    你好啊 ~~~我看你做的是中控的二次开发程序吧  
    小弟也要做,能不能把 你的代码发来我学习下
    谢谢 你  
    我qq是122630695
    email :[email protected]
      

  3.   

    你好啊 ~~~我看你做的是中控的二次开发程序吧  
    小弟也要做,能不能把 你的代码发来我学习下
    谢谢 你  
    我qq是56343562
    email :[email protected]