有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

多线程java。执行多线程数据库应用程序时发生lang.NullPointerException

创建多线程应用程序,该应用程序创建多个线程并查询数据库的同一个表

给定格式为的输入xml文件:

<transliteration>
<element>
    <source>about us</source>
</element>
</transliteration>

应用程序读取多个文件并创建多个线程,每个xml文件一个线程,输出另一个格式为的xml文件

<transliteration>
<element>
    <source>about us</source>
        <target/>
</element>
</transliteration>

下面是线程的run方法

public void run() {

        MultipleDatabaseThread th = new MultipleDatabaseThread();
        Map<String,String> map = new HashMap<String,String>();

        try
        {
            Document doc = loadXmlContentToMemory(this.inputString);

            XPathExpression expr = null;
            XPathFactory xFactory = XPathFactory.newInstance();
            XPath xPath = xFactory.newXPath();
            expr = xPath.compile("/transliteration/element//source");
            Object result = expr.evaluate(doc, XPathConstants.NODESET);
            NodeList nodes = (NodeList) result;
            String sourceString = "";
            if(nodes.getLength() > 0)
            {
                for (int i=0; i<nodes.getLength();i++)
                {
                    //System.out.println("Name: "+nodes.item(i).getNodeName() +" Local Name: "+nodes.item(i).getLocalName() +" Value: "+nodes.item(i).getTextContent());
                    sourceString = nodes.item(i).getTextContent();
                    map = th.getCompleteStringTransliterate(sourceString, this.language);

                    if(map.get(sourceString) == null || map.get(sourceString).equals("") || map.get(sourceString).equals(sourceString))
                    {
                        map.clear();
                        map = th.getRecordsFromDatabase(sourceString, language);

                        Element abc = doc.createElement("target");

                        String targetString = "";

                        String[] tokens = sourceString.trim().split("\\s+");

                        for(int itr=0; itr < tokens.length; itr++)
                        {
                            targetString = targetString+" "+map.get(tokens[itr]);
                        }

                        abc.setTextContent(targetString.trim());
                        nodes.item(i).getParentNode().appendChild(abc);
                    }
                    else
                    {
                       Element abc = doc.createElement("target");
                       abc.setTextContent(map.get(sourceString));
                       nodes.item(i).getParentNode().appendChild(abc);
                    }
                }
            }

            try
            {

                expr = xPath.compile("/transliteration/element//target");
                result = expr.evaluate(doc, XPathConstants.NODESET);
            }catch(XPathExpressionException ex)
            {   }

            NodeList nodesList = (NodeList) result;

            for(int i =0;i<nodesList.getLength();i++)
            {
                System.out.println("Node Name: "+nodesList.item(i).getNodeName()+" Node Value: "+nodesList.item(i).getTextContent());
            }

            try
            {
                Transformer transformer = TransformerFactory.newInstance().newTransformer();
                StreamResult strResult = new StreamResult(new File(this.inputString+"_out.xml"));
                if(doc != null && strResult != null)
                {
                    DOMSource source = new  DOMSource(doc);
                    transformer.transform(source, strResult);
                }
            }
            catch(TransformerException ex)
            {
               System.out.println(""+ex);
            }
            catch(TransformerFactoryConfigurationError ex)
            {
               System.out.println(""+ex);
            }

        }catch(IOException ex)
        {
            ex.printStackTrace(System.out);
        }
        catch(DOMException ex)
        {
            ex.printStackTrace(System.out);
        } 
        catch(ParserConfigurationException ex)
        {
            ex.printStackTrace(System.out);
        }
        catch(SAXException ex)
        {
            ex.printStackTrace(System.out);
        }
        catch(XPathExpressionException ex)
        {
            ex.printStackTrace(System.out);
        }
        catch(InterruptedException ex)
        {
            ex.printStackTrace(System.out);
        }

    }
  • loadXmlContentToMemory**函数将文件名作为输入并加载 文档中的xml内容

  • GetCompleteTringTransiterate**是 MultipleDatabaseThread类,该类返回一个映射变量 包含源及其traget字符串

  • getRecordsFromDatabase**是同一类中的另一个函数 拆分源字符串并再次获取其目标字符串返回映射 变数

    公共类MultipleDatabaseThread{

    public Map<String,String> getCompleteStringTranslate(String inputString, String language) throws InterruptedException
    {
        Map<String,String> map = new HashMap<String,String>();
    
        synchronized(OutputRecords.getMap())
        {
           //long startTime = System.currentTimeMillis();
    
           OutputRecords.clearOutputStream(); 
           Thread thCompleteString = new DatabaseThread(inputString, language);
           thCompleteString.start();
           thCompleteString.join();
    
           map = OutputRecords.getRecords();
           //System.out.println("Complete String Time Taken:: "+(System.currentTimeMillis()-startTime));
           return map;
        }
    }
    
    
    
    public Map<String,String> getRecordsFromDatabase(String inputString, String language) throws  InterruptedException
    {
        String[] tokens = inputString.split("\\s+");
    
        Map<String,String> map = new HashMap<String,String>();
    
        Thread[] databaseThreads = new Thread[tokens.length];
    
        synchronized(OutputRecords.getMap())
        {
            //long startTime = System.currentTimeMillis();
    
            OutputRecords.clearOutputStream();
            for(int index=0; index < tokens.length; index++)
            {
                databaseThreads[index] = new DatabaseThread(tokens[index],language);
                databaseThreads[index].start();
            }
            for(int index = 0 ; index < tokens.length; index++)
            {
                    databaseThreads[index].join();
            }
    
            map = OutputRecords.getRecords();
            //System.out.println("Tokens Time Taken:: "+(System.currentTimeMillis()-startTime));
    
            return map;
    
        }
    }
    

    }

这两个函数都在OutputRecord类中使用一个静态/共享映射变量,并生成多个线程,这些线程实际调用数据库,填充共享映射变量并返回该变量

但在执行这个计划时

Exception in thread "Thread-0" java.lang.NullPointerException
    at transliterationthreading.ExecuteOuterThread.run(ExecuteOuterThread.java:66)

在线

if(map.get(sourceString) == null || map.get(sourceString).equals("") || map.get(sourceString).equals(sourceString))

所以一个线程被终止,另一个线程被完全执行并生成输出文件。我不明白这个问题,谁能给我一些解决这个问题的建议呢

谢谢


共 (2) 个答案

  1. # 1 楼答案

    我怀疑地图内容在评估线条时会发生变化

    if(map.get(sourceString) == null || map.get(sourceString).equals("") || map.get(sourceString).equals(sourceString))
    

    这样,空检查成功,但从映射中获得的新值可能为空。地图不同步

    把这行改成

    String sourceStringValue = map.get(sourceString);
    if(sourceStringValue == null || sourceStringValue.equals("") || map.get(sourceString).equals(sourceString))
    
  2. # 2 楼答案

    谢谢大家的努力

    我尝试使用不同的方法解决这个问题,不使用静态共享映射,而是使用ExecutorService和可调用接口方法

    这是我的密码

    try
            {
                doc = loadXmlContentToMemory(this.inputString);
                expr = xPath.compile("/transliteration/element//source");
                result = expr.evaluate(doc, XPathConstants.NODESET);
    
            }catch(ParserConfigurationException ex)
            {
                System.out.println("loadXmlError: "+ex.toString());
            }
            catch(IOException ex)
            {
                System.out.println("loadXmlError: "+ex.toString());
            }
            catch(SAXException ex)
            {
                System.out.println("loadXmlError: "+ex.toString());
            }
            catch(XPathExpressionException ex)
            {
                System.out.println("loadXmlError: "+ex.toString());
            }
    
            NodeList nodes = (NodeList) result;
            String sourceString = "";
    
            if(nodes.getLength() >0)
            {
                Map<String,String> fileMap = new HashMap<String,String>(); 
                ExecutorService executor = Executors.newFixedThreadPool(NTHREADS);
    
                for(int index =0; index <nodes.getLength(); index++)
                {
                    sourceString = nodes.item(index).getTextContent();
                    Callable<Map<String,String>> worker = new MultipleDatabaseCallable(sourceString,language);
                    Future<Map<String,String>> map = executor.submit(worker);
    
                    try
                    {
                        //System.out.println(""+Thread.currentThread().getName()+"SourceString:: "+sourceString+"Map: "+map.get().get(sourceString));
                          fileMap.putAll(map.get());
                    }catch(InterruptedException ex)
                    {
                        System.out.println("future read: "+ex.toString());
                    }
                    catch(ExecutionException ex)
                    {
                        System.out.println("future read: "+ex.toString());
                    }
                }
    
                executor.shutdown();
                // Wait until all threads are finish
                while (!executor.isTerminated()) {
    
                }
                ExecutorService tokenExecutor = Executors.newFixedThreadPool(NTHREADS);
                for(int i =0 ;i<nodes.getLength();i++)
                {
                    sourceString = nodes.item(i).getTextContent();
                    if(fileMap.get(sourceString) == null || fileMap.get(sourceString).equals("") || fileMap.get(sourceString).equals(sourceString))
                    {
                        fileMap.remove(sourceString);
                        Callable<Map<String,String>> worker = new MultipleTokenCallable(sourceString,language);
                        Future<Map<String,String>> map = tokenExecutor.submit(worker);
    
                        try
                        {
                            fileMap.putAll(map.get());
                        }
                        catch(InterruptedException ex)
                        {
                            System.out.println("Tokenized put Interupted exception: "+ex.toString());
                        }
                        catch(ExecutionException ex)
                        {
                            System.out.println("Tokenized put Execution exception: "+ex.toString());
                            ex.printStackTrace(System.out);
                        }
    
                        Element targetElement = doc.createElement("target");
                        String targetString = "";
    
                        String[] tokens = sourceString.trim().split("\\s+");
    
                        for(int itr=0; itr < tokens.length; itr++)
                        {
                            targetString = targetString+" "+fileMap.get(tokens[itr]);
                        }
                        targetElement.setTextContent(targetString.trim());
                        nodes.item(i).getParentNode().appendChild(targetElement);
                        //System.out.println(""+Thread.currentThread().getName()+" Target:  "+targetString+" Source:  "+sourceString);
                    }
                    else
                    {
                        Element abc = doc.createElement("target");
                        abc.setTextContent(fileMap.get(sourceString));
                        nodes.item(i).getParentNode().appendChild(abc);
                    }
                }
    
                tokenExecutor.shutdown();
                // Wait until all threads are finish
                while (!tokenExecutor.isTerminated()) {
    
                }
                try
                {
                    Transformer transformer = TransformerFactory.newInstance().newTransformer();
                    StreamResult strResult = new StreamResult(new File(this.inputString+"_out.xml"));
                    if(doc != null && strResult != null)
                    {
                        DOMSource source = new  DOMSource(doc);
                        transformer.transform(source, strResult);
                    }
                }
                catch(TransformerException ex)
                {
                   System.out.println(""+ex);
                }
                catch(TransformerFactoryConfigurationError ex)
                {
                   System.out.println(""+ex);
                }
    
            }
    

    由于使用此方法生成多个线程,并且所有线程都试图同时连接到数据库,因此如果并发线程的数量增加,这可能会导致太多连接错误。所以你需要维护一个连接池来解决这个问题