asela38
asela38

Reputation: 4644

Is RemoteServiceServlet in GWT is single Threaded?

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

Answers (1)

Chris Lercher
Chris Lercher

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

Related Questions