user1653773
user1653773

Reputation: 373

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

Upvotes: 1

Views: 427

Answers (1)

Evgheni Crujcov
Evgheni Crujcov

Reputation: 470

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

Upvotes: 2

Related Questions