简体   繁体   English

如何在多个线程中应用程序访问同一数组列表

[英]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? 我是java线程应用程序的新手,请告诉我如何编写程序,每个线程在多线程应用程序中访问相同的对象列表?

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. 通过使用Collections类中的相关方法,可以确保List(或任何Collection)是线程安全的。

From the API : 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. 您可以使用static列表,以便在任何时候只有一个副本。 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. 您不必将共享对象的字段设置为static即可使其成为线程安全的。 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.) 如有必要,您可以将此对象作为参数传递给扩展Thread类的类(或者在匿名类的情况下,甚至可以是封闭类方法中的局部变量)。

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. 或者您可以使用这种方法修饰符使一个方法完全与obj的类synchronized In this case it will be synchronized implicitly and automatically on invocation of the method like "synchronized (this) {..}" block: 在这种情况下,它将在调用“ synchronized(this){..}”块之类的方法时隐式地自动同步:

public void synchronized methodFoo() {
}

As for reading, I read 'Java in a Nutshell', chapter 5.7. 至于阅读,我读了“ Java in a Nutshell”,第5.7章。 "Threads and Concurrency". “线程和并发”。 It was very helpful for me because of overview of all multi-threading possibilities in Java. 由于对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). 在在线资源中,Sun / Oracle的官方教程可能对初学者有所帮助: http : //docs.oracle.com/javase/tutorial/essential/concurrency/ (已在其他答案中提到)。

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

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