我有一个包含以下代码的课程,
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());
}
}
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);
}
}
跑步者类调用DocTransformer。我可以在DocTransformer中访问WDoc,因为它是从runner类调用的,并且对象从它传递给DocTransformer。但我需要访问IndexableDocument中的WDoc对象。怎么实现呢?请解释我的问题是否需要改写,因为我对线程很新。
答案 0 :(得分:0)
您可以为此WDOC使用Exchanger创建一个getter和Setter,并且不要忘记make WDOC final。
Exchanger<WDoc> exchanger = new Exchanger<WDoc>(); \\do it inside some Cover Thread(mb your main class)
currentWdoc = exchanger.exchange(doc); \\ inside getter
如果您仅将WDOC用于读取。你可以使用getter和final field