Reputation: 1
I programmed a simple webserver in Java SE, without using any API. It has a GUI where you can start/stop server and see how many threads it uses.
One of requests from our teacher is that it must support multithreading, and you can control maximum threads before starting the server.
I have WebServer
class that extends Thread
and it runs as one thread. It has fields MAX_THREADS and a thread_counter.
WebServer:
public static final int MAX_THREADS = 5;
public static int threads_counter = 0;
public void run() {
while (isRunning) {
try {
Socket socket = _serverSocket.accept();
if (threads_counter >= MAX_THREADS) {
Thread.sleep(100);
System.out.println("OVERLOAD");
} else {
new Handler(socket, new File(rootDir)).start();
}
} catch (IOException ex) {
Logger.getLogger(WebServer.class.getName()).log(Level.SEVERE, null, ex);
} catch (InterruptedException ex) {
Logger.getLogger(WebServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
There is Handler class which also extends Thread
. I want to limit the number of Handler
s to MAX_THREADS.
Here is how Handler looks:
public void run() {
synchronized (WebServer.class) {
WebServer.threads_counter++;
}
this.setName("thread no. " +WebServer.threads_counter);
System.out.println(this.getName());
HandleRequest(_socket);
synchronized (WebServer.class) {
WebServer.threads_counter--;
}
}
This does not always work as expected, however. Sometimes, it starts more than MAX_THREADS Handler
s and I don't know how to solve it.
Could you please help me with my situation?
Upvotes: 0
Views: 2830
Reputation: 11032
You are using threads_counter
outside a synchronized
block, and this field is not volatile: you have no guarantee to read the "real" value of threads_counter
.
However, like said in comments/answers, you should not create yourself the threads, but use a ThreadPool
or an ExecutorService
. It's always better to reuse such tools instead of reimplementing it.
Upvotes: 1
Reputation: 14678
I would suggest to use ExecutorService instead of creating threads by hand, more specifically Executors.newFixedThreadPool.
That way, you have more control over execution.
On other hand, in your current code, replace synchronized block and incrementing of static variable with AtomicInteger.
In spare time, take a look at java.util.concurrent package, it contains lots of great stuff usable for multithreading.
Upvotes: 4