Example 17-3 Producer/Consumer Socket Program
// ServerProducerConsumer/Server.java
A simple server program.  It sets up a TCP port for the client
program to connect to. Then it accepts connections, spawning a
new producer thread for each. [Java has no "select()" function.]
It starts up nConsumers consumer threads to pull requests off the
list and process them, sending a reply string back to the client.
Any IO failures are handled by printing out an error message,
the socket in question, then ignoring it.  Check out the location
the exception handlers and which methods throw exceptions. This is
carefully designed and *should* be fully robust.
This version is really just a producer/consumer program that
to run across a socket.
import java.io.*;
import java.net.*;
import Extensions.*;
public class Server {
static int
port = 6500;
static int
delay = 10;
static int
spin = 10;
static boolean DEBUG = false;
static int
nConsumers = 10;
static int
static boolean KILL = false;
public static void main(String[] args) {
Server server = new Server();
Thread t;
int stopperTimeout = 10;
// 10s
if (args.length > 0) {
port = Integer.parseInt(args[0]);
if (args.length > 1) {
delay = Integer.parseInt(args[1]);
if (args.length > 2) {
spin = Integer.parseInt(args[2]);
if (args.length > 3) {
nConsumers = Integer.parseInt(args[3]);
if (args.length > 4) {
stopperTimeout = Integer.parseInt(args[4]);
Search WWH :
Custom Search
Previous Page
Multithreaded Programming with JAVA - Topic Index
Next Page
Multithreaded Programming with JAVA - Bookmarks