简体   繁体   中英

Java, Thread - Synchronized variable

How do I create a common variable between threads? For example: Many threads sending a request to server to create users.

These users are saved in an ArrayList , but this ArrayList must be synchronized for all threads. How can I do it ?

Thanks all!

If you are going to access the list from multiple threads, you can use Collections to wrap it:

List<String> users = Collections.synchronizedList(new ArrayList<String>());

and then simply pass it in a constructor to the threads that will use it.

I would use an ExecutorService and submit tasks to it you want to perform. This way you don't need a synchronized collection (possibly don't need the collection at all)

However, you can do what you suggest by creating an ArrayList wrapped with a Collections.synchronizedList() and pass this as a reference to the thread before you start it.

What you could do is something like

// can be reused for other background tasks.
ExecutorService executor = Executors.newFixedThreadPool(numThreads);

List<Future<User>> userFutures = new ArrayList<>();
for( users to create )
   userFutures.add(executor.submit(new Callable<User>() {
        public User call() {
            return created user;
        }
   });
List<User> users = new ArrayList<>();
for(Future<User> userFuture: userFutures)
   users.add(userFuture.get();

To expand on @Peter's answer, if you use an ExecutorService you can submit a Callable<User> which can return the User that was created by the task run in another thread.

Something like:

// create a thread pool with 10 background threads
ExecutorService threadPool = Executors.newFixedThreadPool(10);
List<Future<User>> futures = new ArrayList<Future<User>>();
for (String userName : userNamesToCreateCollection) {
    futures.add(threadPool.submit(new MyCallable(userName)));
}
// once you submit all of the jobs, we shutdown the pool, current jobs still run
threadPool.shutdown();

// now we wait for the produced users
List<User> users = new ArrayList<User>();
for (Future<User> future : futures) {
    // this waits for the job to complete and gets the User created
    // it also throws some exceptions that need to be caught/logged
    users.add(future.get());
}
...

private static class MyCallable implements Callable<User> {
    private String userName;
    public MyCallable(String userName) {
        this.userName = userName;
    }
    public User call() {
        // create the user...
        return user;
    }
}

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