Reputation: 4644
I wrote simple servlet but it seems to be working as a single thread application
public class GreetingServiceImpl extends RemoteServiceServlet implements
GreetingService {
private static int i = 0;
public String greetServer(String input) {
if(i%2 == 0) {
try {
Thread.currentThread().sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
try {
Thread.currentThread().sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//System.out.println(this.getThreadLocalRequest().getSession().getId());
System.out.println(Thread.currentThread().getId());
return String.valueOf(i++);
}
}
in server log I get two different id's like 13 and 28 when requests are send from two different browser instances. Every time it takes more than 10s.
Upvotes: 1
Views: 865
Reputation: 37778
No, it's not single threaded. But you increase i
only after the 10 seconds have passed.
So Browser1 calls the service, i == 0
. => sleeps 10 seconds.
Then Browser2 calls the service during these 10 seconds, and still i == 0
. => also sleeps 10 seconds.
Note
If you actually want to make your servlet instances single threaded, you can use the SingleThreadModel - but that only affects instance fields, not static fields.
Upvotes: 1