在多线程方面需要帮助

欧比湾-PallavJha

我正在创建一个像应用程序这样的搜寻器,它将在网页中找到图像。在这里,生产者产生链接,而消费者连接到该链接以查找图像,但是由于消费者产生了大量的链接,消费者花费了大量时间。因此,我将消费者置于执行者服务中,但是我发现消费者所花费的时间没有减少。请帮我。下面是我的代码。

@Service
@Qualifier("crawlerService")
public class CrawlerService {

@Autowired
@Qualifier("loggerService")
LoggerService loggerService;

@Autowired
@Qualifier("imageTypeExtensionCombo")
ImageTypeExtensionCombo imageTypeExtensionCombo;

public List<String> startCrawler(List<String> links, List<String> images, URL url, String protocol, String protocolHost) throws Exception{
    LinkQueue queue = new LinkQueue(links);
    LinkProducer producer = new LinkProducer(links, url, protocol, protocolHost, queue, loggerService);
    LinkConsumer consumer = new LinkConsumer(links, images, url, protocol, protocolHost, loggerService, queue);
    ExecutorService executorService = Executors.newFixedThreadPool(4);
    executorService.submit(consumer);
    producer.start();
    //consumer.start();
    Thread.currentThread().join();
    executorService.shutdown();
    return images;
  }
}

LinkProducer类

public class LinkProducer extends Thread {

    private List<String> anchorList;
    private URL url;
    private String protocol;
    private String protocolHost;
    private UrlValidator urlValidator = new UrlValidator();
    private LinkQueue queue;
    private LoggerService loggerService;
    private int MAX_QUEUE_SIZE = 2;
    private int counter = 0;
    private boolean stopThread = false;

    private String HTML_TYPE = "HTML";
    private String HTML_CONTENT_TYPE = "text/html";
    private String IMAGE_TYPE = "IMAGE";
    private String NON_HTML_NON_IMAGE_TYPE = "OTHERS";

    public LinkProducer(List<String> anchorList, URL url, String protocol,String protocolHost, LinkQueue queue, LoggerService loggerService) {

        super(protocolHost.replace(protocol, "").replaceAll("/", ""));
        this.anchorList = anchorList;
        this.url = url;
        this.protocol = protocol;
        this.protocolHost = protocolHost;
        this.queue = queue;
        this.loggerService = loggerService;

    }


    public void run() {
        int i = 0;
        while(true) {
            List<String> anchors = null;
            loggerService.log("Producer Thread : " + (++i));
            try {
                anchors = produce();
            } catch (Exception ex) {
                loggerService.log("Exception occured in producer thread : "+ ex.getMessage());
                ex.printStackTrace();
                if(stopThread){
                    break;
                }
            }
            if(stopThread){
                break;
            }
            if(anchors != null && anchors.size() > 0){
                Iterator<String> iter = anchors.iterator();
                while(iter.hasNext()){
                    synchronized (queue) {
                        queue.enQueue(iter.next());
                    }
                }
            }
        }
    }
 }

LinkConsumer类

public class LinkConsumer extends Thread {

    private List<String> anchorList;
    private List<String> imageList;
    private URL url;
    private String protocol;
    private String protocolHost;
    private LinkQueue queue;
    private LoggerService loggerService;
    private UrlValidator urlValidator = new UrlValidator();

    private String HTML_TYPE = "HTML";

    private String HTML_CONTENT_TYPE = "text/html";

    private String IMAGE_TYPE = "IMAGE";

    private String NON_HTML_NON_IMAGE_TYPE = "OTHERS";

    public LinkConsumer(List<String> anchorList, List<String> imageList, URL url, String protocol,String protocolHost, LoggerService loggerService, LinkQueue queue) {

        super(protocolHost.replace(protocol, "").replaceAll("/", ""));
        this.anchorList = anchorList;
        this.imageList = imageList;
        this.url = url;
        this.protocol = protocol;
        this.protocolHost = protocolHost;
        this.queue = queue;
        this.loggerService = loggerService;
    }

    public void run() {
        int  i = 0;
        while (!queue.isEmpty()) {
            List<String> images = null;
            loggerService.log("Consumer Thread : " + (++i));
            try {
                images = consume();
            } catch (Exception ex) {
                loggerService.log("Exception occured in consumer thread : "+ ex.getMessage());
                ex.printStackTrace();
            }
            if (images != null && images.size() > 0) {
                Iterator<String> iter = images.iterator();
                while (iter.hasNext()) {
                    imageList.add(iter.next());
                }
            }
        }
    }
 }

谢谢

托马斯·普兰蒂夫

您仅创建和提交一个LinkConsumer,因此您只有一个工作人员。

为了实现真正的并行性能,您将需要创建和提交更多内容LinkConsumer

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

需要调用/多线程帮助

来自分类Dev

在多线程合并排序方面需要建议

来自分类Dev

Java需要线程/代码编译调试方面的帮助

来自分类Dev

在MAAS方面需要帮助

来自分类Dev

在截断方面需要帮助

来自分类Dev

在环回方面需要帮助

来自分类Dev

需要有关Delphi多线程文件编写的帮助

来自分类Dev

在以下汇编语言方面需要帮助

来自分类Dev

在Python递减方面需要帮助

来自分类Dev

在通用数组创建方面需要帮助

来自分类Dev

在使用R清理数据方面需要帮助

来自分类Dev

在格式化功能方面需要帮助

来自分类Dev

在理解量表方面需要帮助

来自分类Dev

在理解标记模板方面需要帮助

来自分类Dev

需要比赛条件方面的帮助

来自分类Dev

在多层动画位置方面需要帮助

来自分类Dev

在纠正横幅图像尺寸方面需要帮助

来自分类Dev

在共享元素片段过渡方面需要帮助

来自分类Dev

jQuery需要编码方面的帮助

来自分类Dev

CSS需要“格式设置”方面的帮助

来自分类Dev

在PHP curl调用方面需要帮助

来自分类Dev

在加入和条件方面需要帮助

来自分类Dev

在QEMU上的USB直通方面需要帮助

来自分类Dev

在汇总计数功能方面需要帮助

来自分类Dev

需要 XSLT 编码方面的帮助

来自分类Dev

在使用 Charindex 的 TSQL 方面需要帮助

来自分类Dev

Bootstrap - 需要布局方面的帮助

来自分类Dev

我需要 C++ 方面的帮助

来自分类Dev

在 Vigenere Cipher 计划方面需要帮助