简体   繁体   English

将值从一个类传递到另一个可调用类

[英]passing values from a class to another callable class

I have a class with the below code, 我有一个包含以下代码的课程,

public class DocTransformer implements Callable<IndexableDocument> {

    WDoc document;
    public DocTransformer(Map<INDEXFIELD, Tokenizer> tknizerMap, WDoc doc) {
        this.document = doc;
    }

    public IndexableDocument call() throws TokenizerException {
        System.out.println("Inside doctrans: "+this.document.getId());

    }

}

the IndexableDocument looks like below, IndexableDocument如下所示,

public class IndexableDocument {

    WDoc doc;
    public IndexableDocument() {
        System.out.println("this is inside indexable document");
    }

    public void addField(INDEXFIELD field, TStream stream) {
        //TODO: Implement this method
    }

    public TokenStream getStream(INDEXFIELD key) {
        //TODO: Implement this method
        return null;
    }

    public String getDocumentIdentifier() {
        System.out.println(doc.getId);
    }

}

A runner class calls the DocTransformer. 运行器类调用DocTransformer。 I can access WDoc inside the DocTransformer as it is being called from the runner class and the object is passed from it to DocTransformer. 我可以从Runner类调用WDoc并将其从对象传递到DocTransformer,从而在DocTransformer中访问WDoc。 But I need to access the WDoc object inside IndexableDocument. 但是我需要访问IndexableDocument中的WDoc对象。 How to achieve it? 如何实现呢? Please explain if my question needs to rephrased, as I am very new to threads. 请解释我的问题是否需要改写,因为我是线程新手。

You can create a getter and Setter for this WDOC use Exchanger for it, and don't forget make WDOC final. 您可以为此WDOC使用Exchanger创建一个getter和Setter,并且不要忘记使WDOC成为最终版本。

Exchanger<WDoc> exchanger = new Exchanger<WDoc>(); \\do it inside some Cover Thread(mb your main class)
currentWdoc = exchanger.exchange(doc); \\ inside getter

if you nedd WDOC only for read . 如果您仅需阅读WDOC。 you can just Use getter and final field 您可以只使用getter和final字段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM