synchronized only for threads?

If I have a method which can be accessed by many clients on the server( usign rmi) can I just put synchronized in the method without the clients being threads. will it server the purpose?
[208 byte] By [sseans] at [2007-9-26 3:03:22]
# 1

All that synchronized means is that only one thread at a time may execute that code for that instance.

I don't quite understand the question about the client: surely the client won't call the method, the web server will do that? Without some ocde or more details I can't really answer the question.

alan.mck at 2007-6-29 11:03:17 > top of Java-index,Core,Core APIs...
# 2

no actually the client will call the method because I am using rmi.

eg a login and regstering function on the server which takes a players name . The client fills his name on the screen and invokes the method on the server.

Now many clients can cal lthe server simultaenously.

So does the method has to be synchronised. Now if the object (client ) that calls the method is not a thread ,

and I put

synchronised public void register()

will it take care of only one client being active in the method at a time.

sseans at 2007-6-29 11:03:17 > top of Java-index,Core,Core APIs...
# 3

i think

the synchronization will be done for all the callers

either directly a thread or not

because any call that you make is a seprate runnig sequence of instructions

try this code

whith two different calls to print and print1 Methods

public class Test {

public Test() {

Thread th1=new Thread(new Runner());

Thread th2=new Thread(new Runner());

th2.start();

th1.start();

print(3);

}

public static void main(String[] args) {

Test theradTest = new Test();

}

int i=0;

class Runner implements Runnable{

public void run(){

print(i++);

}

}

synchronized void print(int j){

for (int i=0;i<100;i++)

System.out.println("i"+i+"j"+j);

}

void print1(int j){

for (int i=0;i<100;i++)

System.out.println("i"+i+"j"+j);

}

}

eharain at 2007-6-29 11:03:17 > top of Java-index,Core,Core APIs...
# 4
a separate running sequence , yes , but sharing the same varaiables leading to race conditions?
sseans at 2007-6-29 11:03:17 > top of Java-index,Core,Core APIs...
# 5
You can either synchronize the methods, or you can have the methods synchronize on the data objects.Basically it boils down to whether any of the calls can usefully work in parallel. If not, then just synchronize the methods.
bschauwe at 2007-6-29 11:03:17 > top of Java-index,Core,Core APIs...
# 6

1. If the clients are not threads, they can only access the object 1 at a time anyway.

2. You can consider a seperate running process (client) as a Seperate thread, because it is.

3. I dont think RMI allows simultaneous invocation anyway, so your RMI server will only allow 1 client at a time to invoke a method.

4. Putting synchronized will INSURE that the synchronized resource can only be accesses by 1 at a time, but I am almost positive this will be redundent.

dnoyeB at 2007-6-29 11:03:17 > top of Java-index,Core,Core APIs...