Java 多线程多pdf文件

Java 多线程多pdf文件,java,multithreading,parsing,pdf,apache-tika,Java,Multithreading,Parsing,Pdf,Apache Tika,因此,我尝试通过一个函数运行多个PDF文件,该函数可以刮取文本,将其与静态字典进行比较,然后将其关系数据添加到MYSQL中的索引表中。我研究了多线程,但不确定这是否能满足我的需要 这里是for循环,我将在其中浏览所有PDF文件 forString temp:文件{ //地址:康涅狄格州迪克特姆,迪克托尔; //康涅狄格州,康涅狄格州,新Scrapertemp; Scraper obj=新的Scrapertemp,命令式,conn; 螺纹T1=新螺纹OBJ; T1.启动; //System.out

因此,我尝试通过一个函数运行多个PDF文件,该函数可以刮取文本,将其与静态字典进行比较,然后将其关系数据添加到MYSQL中的索引表中。我研究了多线程,但不确定这是否能满足我的需要

这里是for循环,我将在其中浏览所有PDF文件

forString temp:文件{ //地址:康涅狄格州迪克特姆,迪克托尔; //康涅狄格州,康涅狄格州,新Scrapertemp; Scraper obj=新的Scrapertemp,命令式,conn; 螺纹T1=新螺纹OBJ; T1.启动; //System.out.printlnamumuntoffiles-+剩下的文件; } 这是我创建的实现runnable的Scraper类

公共类可运行{ 私有字符串文件路径; 私有哈希映射; 专用连接连接器; 公共ScraperString文件路径,HashMap dict,连接{ //为以后的用户存储参数 filePath=文件路径; map=dict; conn=连接; } @凌驾 公开募捐{ //剪切文件路径,使其从数据文件夹开始 int-cutPos=filePath.indexOfData; String cutPath=filePath.substringcutPos; cutPath=cutPath.replaceAll\\\\,|; 系统输出打印路径+报废; //询问 String addSentanceQuery=插入到sentancesentance\u ID,sentance\u值默认值,?; String addContextQuery=插入到contextcontext\u ID、word\u ID、sentance\u ID、pdf\u路径值默认值、、、、,?,?,?; //准备好的声明 //结果集 ResultSet sentanceKeyRS=null; BodyContentHandler=新的BodyContentHandler-1; 元数据=新元数据; FileInputStream inputstream=null; 试一试{ inputstream=新文件InputStreamNew FilePath; }捕获文件NotFoundException ex{ Logger.getLoggerScraper.class.getName.logLevel.SEVERE,null,ex; } ParseContext pcontext=新的ParseContext; //使用PDF解析器解析文档 PDFParser PDFParser=新PDFParser; 试一试{ pdfparser.parseinputstream,处理程序,元数据,pcontext; }捕捉异常{ Logger.getLoggerScraper.class.getName.logLevel.SEVERE,null,ex; }捕获SAXEx异常{ Logger.getLoggerScraper.class.getName.logLevel.SEVERE,null,ex; }抓住提卡{ Logger.getLoggerScraper.class.getName.logLevel.SEVERE,null,ex; } //获取文档的内容 字符串fileText=handler.toString; fileText=fileText.toLowerCase; //按新行拆分文本 字符串语句[]=fileText.split\\n; forString x:语句{ x=x.trim; ifx.isEmpty | | x.matches\\t+| | x.matches\\n+| | x.matches{ }否则{ int sentanceID=0; //将sentance添加到db并获取id 请尝试PreparedStatementAddSentancePrepare=conn.prepareStatementaddSentanceQuery,Statement.RETURN\u生成的\u键{ addSentancePrepare.setString 1,x; addSentancePrepare.executeUpdate; sentanceKeyRS=addSentancePrepare.getGeneratedKeys; 而下一个{ sentanceID=sentanceKeyRS.getInt1; } addSentancePrepare.close; sentanceKeyRS.close; }捕获SQLException-ex{ Logger.getLoggerScraper.class.getName.logLevel.SEVERE,null,ex; } 字符串字[]=x.split; forString y:文字{ y=y.trim; ify.matches\\s+| y.matches{ }否则ifmap.containsKeyy{ //获取ID并放在中间表中 请尝试PreparedStatement addContextPrepare=conn.prepareStatementaddContextQuery{ addContextPrepare.setInt1,map.gety; addContextPrepare.setInt2,sentanceID; addContextPrepare.setString 3,剪切路径; addContextPrepare.executeUpdate; addContextPrepare.close; }捕获SQLException-ex{ Logger.getLoggerScraper.class.getName.logLevel.SEVERE,null,ex; } } } } } 试一试{ inputstream.close; }捕捉异常{ Logger.getLoggerScraper.class.getName.logLevel.SEVERE,null,ex; } } }
我这样做对吗?我从未使用过多线程,但它似乎可以加快我的程序。

您已经完成了程序的基本建模。从概念上讲,你几乎做到了 T但很少有人担心

可伸缩性 当您需要处理更多的文件时,您不能简单地增加线程的数量。尽管增加兼职员工的数量应该会像我们感觉的那样提高绩效,但在现实世界中可能并非如此。当线程数增加到一定程度时,性能实际上会下降,这取决于线程争用、通信和内存使用。所以我;我建议您使用java并发包附带的ThreadPool实现。请参阅我对您的代码所做的以下修改

public class Test {

    private final ThreadPoolExecutor threadPoolExecutor;

    public Test(int coreSize, int maxSize) {
        this.threadPoolExecutor = new ThreadPoolExecutor(coreSize,maxSize, 50, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(100));
    }


    public void submit(String[] files) {
        for(String temp: files){
            //addToDict(temp,dictonary,conn);
            //new Scraper(temp,dictonary,conn).run();
            Scraper obj=new Scraper(temp,dictonary,conn);
            threadPoolExecutor.submit(obj);

            //System.out.println((ammountOfFiles--)+" files left");
        }
    }

    public void shutDown() {
        this.threadPoolExecutor.shutdown();
    }
}
线程安全和同步 我可以看到您已经跨线程共享了java.sql.Connection实例。尽管java.sql.Connection是线程安全的,但这种用法会显著降低应用程序的性能,因为java.sql.Connection通过同步实现线程安全。因此,一次只能有一个线程使用该连接。为了克服这个问题,我们可以使用连接池概念。我可以建议一个简单的例子是
多线程不一定能提高任何速度。如果您有可以同时运行的CPU密集型任务,它会有所帮助。这并不是一个神奇的子弹,使所有事情总是更快,有时它甚至使事情更慢。