简体   繁体   中英

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. 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 :(

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.

This could help with an example: http://www.vogella.com/articles/JavaConcurrency/article.html#futures

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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