简体   繁体   中英

How to Application access same arraylist in more than one thread

I am new to java thread application please tell me How to write program, every thread access same list of object in multithreading application?

is there any good link to read?

You can make sure your List (or any Collection) will be thread safe by using the relevant methods in the Collections class.

From the API :

public static <T> List<T> synchronizedList(List<T> list)
    Returns a synchronized (thread-safe) list backed by the specified list.

For example

static List mySharedList = Collections.synchronizedList(new ArrayList());

尝试使用相同的实例访问列表,或使其变为静态并使其同步,以使列表成为线程安全的。

You can use a static list so that there would be only one copy at any time. Also make sure to use syncronised methods for thread-safe.

To access the same instance of your list from all threads, make it static. Eg:

private static List myList;

Then make the accessing method thread-safe (ie Make it so that only one thread can access it at one time, so as to avoid conflicts). Eg:

public static synchronized updateList(String parameters) {
    // Do something
}

Yes, all threads are able to access the same instance of any objects (incl. classes). Because a memory space is created on per-application (ie per-process) basis. Then a process contains all threads inside, incl. implicit 'main' one, with shared memory space.

If an object is used in one thread only, there are no any concurrency issues. You need no any 'synchronization', locking etc. But sometimes you may have to share something between thread. If both reading and writing can be done in a few threads simultaneously, it means you need synchronize by this object to deal with so called 'racing'.

You don't have to make a field as static for a shared object to make it thread-safe. If necessary, you can just pass this object as a parameter into a class which extends Thread class (or it may be even a local variable in enclosing class method in case of anonymous class, etc.)

So all you need is just synchronize by this object. You can synchronize either explicitly inside a method:

synchronized (obj) {
    // doing a thread-safe stuff
}

or you can make a method synchronized entirely for an obj's class using such method modifier. In this case it will be synchronized implicitly and automatically on invocation of the method like "synchronized (this) {..}" block:

public void synchronized methodFoo() {
}

As for reading, I read 'Java in a Nutshell', chapter 5.7. "Threads and Concurrency". It was very helpful for me because of overview of all multi-threading possibilities in Java.

Among online resources, official Sun/Oracle's tutorial may be helpful for the beginners: http://docs.oracle.com/javase/tutorial/essential/concurrency/ (which has been already mentioned in another answers).

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