简体   繁体   English

Java线程访问对象

[英]Java Threading accessing object

I have a function which in executing a thread 我有一个执行线程的功能

Public Class Connector{

private static void anyFileConnector() {
            // Starting searching Thread
            traverse.executeTraversing();
    }

} }

public class Traverse {
private synchronized void executeTraversing(Path dir) {
                ExecutorService executor = Executors.newFixedThreadPool(1);
                // Doing some operation
                                        executor.submit(newSearch);
                                }
                        }
                } catch (IOException | DirectoryIteratorException x) {
                        // IOException can never be thrown by the iteration.

                } finally {
                        executor.shutdown();
                }
        }
}

    public class Search implements Runnable {
            private List<ResultBean> result = new ArrayList<ResultBean>();

            public void run() {
                synchronized (Search.class) {
// In this Search Method i am setting above list "result"
                        this.search(); 

                }
        }

I want "result" object with values in my Connector class just after the "executeTraversing" method with least code change. 我希望在具有最少代码更改的“ executeTraversing”方法之后紧接我的Connector类中的“结果”对象。 What is the best way to get it there. 到达那里的最好方法是什么。

All the above classes are in different packages. 以上所有类均在不同的程序包中。

I dont know, how can i google it also :( 我不知道,我怎么也可以用Google :(

If you want your thread to return a value (that is, the object you desire) you should look into refactoring your objects to implement Callable() instead of Runnable(), and use a Future class to return a value. 如果希望线程返回值(即所需的对象),则应考虑将对象重构为实现Callable()而不是Runnable(),并使用Future类返回值。

This could help with an example: http://www.vogella.com/articles/JavaConcurrency/article.html#futures 这可能有助于举个例子: http : //www.vogella.com/articles/JavaConcurrency/article.html#futures

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

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