我有那个代码:
主类:
public class myTest {
public static void main(String[] args) {
try {
Thread t1 = new Thread(new myThreadClass("thread 1"), "thread 1");
t1.start();
} catch (UnknownHostException ex) {
Logger.getLogger(glownyTest.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(glownyTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
我的线程类
public class myThreadClass extends Thread {
private HashSet<String> texts = new HashSet<String>();
public myThreadClass(String id) throws UnknownHostException, IOException {}
@Override
public void run() {
... collecting Strings into my hashSet ....
}
public HashSet<String> getTexts() {
return texts;
}
}
我试过打电话
t1.getTexts();
在启动线程后在我的主类中,但它不起作用 - 我想从我的主类级别访问文本 hashSet。如何实现?