In my experience it is not common for you to have two independent lists requiring synchronized access to an object. It may be evidence of a violation of the Single Liability Principle. But this is a design issue and does not come to the point.
Keep in mind that the synchronized access to a code snippet depends on a "token", that is, an object of which there is only one instance, so that whoever has it at any given time can perform the desired action, otherwise you will have to wait for the owner of the token release him so he can acquire that token and perform the action. Except that the term used is not "token" but "lock" (or semaphore, monitor, etc.). What I mean is that his job is to be a token. This is what synchronized access is all about: the thread who owns the lock can enter the synchronized code snippet and execute it (and perform operations on your lists, for example).
Any object can be a "lock". When a method is synchronized
, the lock is the very object that contains that method. In the case of your example, if you have such a code...
Objt bijeto = new Objt();
bijeto.handleLists(list);
...the object bijeto
lock for the synchronized method handleLists()
.
The choice of lock (or of Locks) most appropriate(s) for your case will depend on your intention. When a thread has access to list_t1
, you want him to also have access to list_t2
? In this case, you can use a lock only for both lists. This lock can be the object itself Objt
, or another object you define as an attribute of Objt
.
Or when a thread has access to list_t1
, you don’t want him to have access to list_t2
? Then you’ll need two Locks different. The lists themselves, if they are non-null and declared as final
, can be used as Locks for access to themselves. Or else you can create two attributes lock_t1
and lock_t2
.
Note that the two code snippets below are equivalent:
Stretch 1:
public class Objt {
public synchronized handleList() {
...
}
}
Stretch 2:
public class Objt {
public handleList() {
synchronized(this) { // "this" é referência para o próprio
... // objeto de classe Objt
}
}
}
Editing:
You clarified that you want to have a method that takes a list as a parameter and makes changes to it. A thread will call this method by passing a list list_t1
as a parameter and another thread will call this method by passing another list list_t2
as a parameter.
In this case IF this is the only point where the lists are modified, the parallelism will work smoothly and there will be no need to declare the method synchronized
, as @Maniero said in his reply, which perfectly answers the question. It will work because the method is not making any changes in shared state, only in the state of the parameters passed to it, which in this case are independent of each other.
But avoid declaring the lists in the class that contains this same method, this only creates confusion (because if they are attributes of a class it would not be necessary to pass them as a parameter to methods of that same class).
Or else stop declaring everything as public
and try to learn how to use Java access modifiers to correctly reflect your intentions in the code.
If you want to make the code more elegant, give more details about what you want to do and include more code in the question, including threads who call such a method, that we show you how it can stay.
The question does not make clear what you really want, what the specific situation is, but if I have understood what is written, the
synchronized
seems unnecessary. Why would you need to create another method to do the same thing? Create each thread calling the same method by passing each list in a thread.– Maniero
Dude, the situation is this presented, I don’t have much to add. I need to manipulate the lists of the same object in "parallel". The
synchronized
is there assuming that I implemented the case of two threads using the same method of that object each passing a list. With the solution you talked about that will give problem, threads will change the reference to variablelist
of the method all the time, and so I will get a completely undesirable result, because I call the same method of the same object every time.– Christian Felipe
If there are two different lists there is no reason why one should interfere with the other. I think you have not understood how the threads work. Or else you are reporting a completely different problem than you really want and want to mess with the same list in two threads. But it is not what is written. And if it is also, you probably will not have the gain you are waiting for. Read: http://answall.com/q/1946/101. And read Piovesan’s reply confirming this. He considered that the code will use the two lists in the method, I think not (I think because the code was not shown, so have what to add).
– Maniero
I added more details after evaluating some things here man!
– Christian Felipe