我应该如何设计这个程序?

我正在用 Java 编写程序。图片不言自明——

http://img4.mukewang.com/62bd830b0001dfe106470422.jpg

main 方法产生三个线程。SAX 处理器处理输入的 XML 文件,生成 JAXB 对象并将它们放入 guava 缓存中。Guava 缓存由另一个线程处理。每当任何对象进入缓存时,该线程都会通知第三个线程,即 MDL 生成器(它关联类似的 JAXB 对象,将它们互连并生成另一个 XML 文件,称为 MDL)。我为主要课程编写了以下代码 -


package test;


import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;


public class MainClass {


    public static void main(String args[]) {


        ExecutorService xMLService = Executors.newFixedThreadPool(1);

        xMLService.execute(new XMLProcessor());


        ExecutorService cacheService = Executors.newFixedThreadPool(1);

        cacheService.execute(new CacheProcessor());


        ExecutorService mdlService = Executors.newFixedThreadPool(1);

        mdlService.execute(new MDLProcessor());


        xMLService.shutdown();

        cacheService.shutdown();

        mDLService.shutdown();

    }

}

但是现在我对如何在线程之间传递对象以及如何在新对象进入缓存时通知 MDL 生成器有疑问。在 Java 旧线程模型中,我们可以使用 notify(),但我想使用当前的 ExecutorService。并且有异步回调。所以我想知道如何设计这个框架。如何传递对象并通知线程?我们将缓存对象保存在 HashMap 中,并且 CacheService 线程需要将密钥传递给 MDLService。那么我应该使用哪种模式?


开满天机
浏览 110回答 3
3回答

一只斗牛犬

如何传递对象并通知线程?我们将缓存对象保存在 HashMap 中,并且 CacheService 线程需要将密钥传递给 MDLService。那么我应该使用哪种模式?在我看来,你有 1 个线程太多了。XML 读取线程和 MDL 写入是有意义的,但是仅将内容放入内存缓存的线程似乎太复杂了。如果 MDL 生成器需要使用 Guava 缓存,那么它应该“拥有”缓存并将内容粘贴到其中。这样就剩下 1 个输入 SAX 处理器线程和一个输出 MDL 生成器线程。好的。要连接两者,我会使用BlockingQueue类似的LinkedBlockingQueue. 您可能希望也可能不希望对队列设置大小限制,具体取决于读取是否比写入快以及您的工作中有多少记录。所以你的主线程将创建BlockingQueue然后将它传递给输入和输出线程。SAX 输入线程调用put()队列,MDL 输出线程调用take()将对象放入 Guava 缓存,然后生成 MDL。希望这可以帮助。

Helenr

由于您使用的是 Guava Cache,因此您可以使用 Guava AsyncEventBus 在任务之间调度消息,并取消三个单独的专用 ExecutorServices。

HUWWW

这是上述情况的示例实现。请注意,即使没有 Guava 缓存也可以实现,正如其他一些回复的人所提到的那样;尽管如此,我认为 Nirmalaya 要求它可能是有正当理由的。我可以想到的一个这样的原因是将缓存溢出到存储设备或数据库,以节省运行时内存。员工记录.xml<?xml version="1.0" encoding="UTF-8"?><Employees>&nbsp; &nbsp; <Employee id="1">&nbsp; &nbsp; &nbsp; &nbsp; <name>Thomas</name>&nbsp; &nbsp; </Employee>&nbsp; &nbsp; <Employee id="2">&nbsp; &nbsp; &nbsp; &nbsp; <name>Lisa</name>&nbsp; &nbsp; </Employee>&nbsp; &nbsp; <Employee id="3">&nbsp; &nbsp; &nbsp; &nbsp; <name>Ronald</name>&nbsp; &nbsp; </Employee>&nbsp; &nbsp; <Employee id="4">&nbsp; &nbsp; &nbsp; &nbsp; <name>Erica</name>&nbsp; &nbsp; </Employee></Employees>Employee.javapackage com.technoroy.examples.guava;/**&nbsp;* A value holder POJO implementation for Employee records&nbsp;* @author Rahul R&nbsp;*&nbsp;*/class Employee {&nbsp; &nbsp; private Integer id = null;&nbsp; &nbsp; private String name = null;&nbsp; &nbsp; public Employee() {&nbsp; &nbsp; &nbsp; &nbsp; super();&nbsp; &nbsp; }&nbsp; &nbsp; public Employee(Integer id, String name) {&nbsp; &nbsp; &nbsp; &nbsp; super();&nbsp; &nbsp; &nbsp; &nbsp; this.id = id;&nbsp; &nbsp; &nbsp; &nbsp; this.name = name;&nbsp; &nbsp; }&nbsp; &nbsp; public Integer getId() {&nbsp; &nbsp; &nbsp; &nbsp; return id;&nbsp; &nbsp; }&nbsp; &nbsp; public void setId(Integer id) {&nbsp; &nbsp; &nbsp; &nbsp; this.id = id;&nbsp; &nbsp; }&nbsp; &nbsp; public String getName() {&nbsp; &nbsp; &nbsp; &nbsp; return name;&nbsp; &nbsp; }&nbsp; &nbsp; public void setName(String name) {&nbsp; &nbsp; &nbsp; &nbsp; this.name = name;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public String toString() {&nbsp; &nbsp; &nbsp; &nbsp; return "Employee [id=" + id + ", name=" + name + "]";&nbsp; &nbsp; }}GuavaCacheProcessor.javapackage com.technoroy.examples.guava;import java.io.IOException;import java.io.InputStream;import java.util.concurrent.BlockingQueue;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.LinkedBlockingQueue;import javax.xml.parsers.ParserConfigurationException;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;import org.xml.sax.Attributes;import org.xml.sax.SAXException;import org.xml.sax.helpers.DefaultHandler;import com.google.common.cache.Cache;import com.google.common.cache.CacheBuilder;/**&nbsp;* The primary executable class&nbsp;*&nbsp;&nbsp;* @author Rahul R&nbsp;*&nbsp;*/public class GuavaCacheProcessor {&nbsp; &nbsp; private final static BlockingQueue<Integer> notificationQueue = new LinkedBlockingQueue<>();&nbsp; &nbsp; public static void main(String... arguments) {&nbsp; &nbsp; &nbsp; &nbsp; Runnable xmlProcessor = new Runnable() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void run() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parseDataFile();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; Runnable mdlGenerator = new Runnable() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void run() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (true) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Integer id = notificationQueue.take();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Employee record = ApplicationCacheUtil.getRecord(id);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; generateContent(record);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (InterruptedException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Thread.currentThread().interrupt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; ExecutorService executorService = Executors.newFixedThreadPool(2);&nbsp; &nbsp; &nbsp; &nbsp; executorService.submit(xmlProcessor);&nbsp; &nbsp; &nbsp; &nbsp; executorService.submit(mdlGenerator);&nbsp; &nbsp; }&nbsp; &nbsp; public static void generateContent(Employee employee) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(employee);&nbsp; &nbsp; }&nbsp; &nbsp; public static void parseDataFile() {&nbsp; &nbsp; &nbsp; &nbsp; SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();&nbsp; &nbsp; &nbsp; &nbsp; InputStream dataInputStream = GuavaCacheProcessor.class.getResourceAsStream("employee-records.xml");&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SAXParser saxParser = saxParserFactory.newSAXParser();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; saxParser.parse(dataInputStream, new DefaultHandler() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; private Employee employee = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; private StringBuilder elementValue = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void startElement(String uri, String localName, String qName, Attributes attributes)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throws SAXException {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (qName.equalsIgnoreCase("Employee")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; employee = new Employee();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String id = attributes.getValue("id");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (id.matches("-?\\d+(\\.\\d+)?")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; employee.setId(Integer.valueOf(id));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elementValue = new StringBuilder();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void characters(char ch[], int start, int length) throws SAXException {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (elementValue != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elementValue.append(new String(ch, start, length));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void endElement(String uri, String localName, String qName) throws SAXException {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (qName.equalsIgnoreCase("name")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (employee != null && elementValue != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; employee.setName(elementValue.toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (qName.equalsIgnoreCase("Employee")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ApplicationCacheUtil.putRecord(employee.getId(), employee);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; notificationQueue.put(employee.getId());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (InterruptedException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elementValue = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; } catch (ParserConfigurationException | SAXException | IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}/**&nbsp;* The Cache utilities class, that initializes and returns a handle to the&nbsp;* cache.&nbsp;*&nbsp;&nbsp;* @author Rahul R&nbsp;*&nbsp;*/class ApplicationCacheUtil {&nbsp; &nbsp; private static Cache<Integer, Employee> cache = CacheBuilder.newBuilder().build();&nbsp; &nbsp; public static Cache<Integer, Employee> getCache() {&nbsp; &nbsp; &nbsp; &nbsp; return cache;&nbsp; &nbsp; }&nbsp; &nbsp; public static void putRecord(Integer key, Employee value) {&nbsp; &nbsp; &nbsp; &nbsp; cache.put(key, value);&nbsp; &nbsp; }&nbsp; &nbsp; public static Employee getRecord(Integer key) {&nbsp; &nbsp; &nbsp; &nbsp; return cache.getIfPresent(key);&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java