J.E.D.I.
Creates a thread pool that reuses a fixed set of threads operating off a shared unbounded queue. An overloaded method, which takes in a ThreadFactory object as
an additional parameter. public static ScheduledExecutorService newScheduledThreadPoolint
corePoolSize Creates a thread pool that can schedule commands to run after a given delay, or to
execute periodically. An overloaded method, which takes in a ThreadFactory object as an additional parameter.
public static ExecutorService newSingleThreadExecutor Creates an Executor that uses a single worker thread operating off an unbounded
queue. An overloaded method, which also takes in a ThreadFactory object as an argument.
public static ScheduledExecutorService newSingleThreadScheduledExecutor Creates a single-threaded executor that can schedule commands to run after a given
delay, or to execute periodically. An overloaded method, which also takes in a ThreadFactory object as an argument.
Table 33: Factory Methods of the Executors Class
The Runnable tasks are executed and finished under the control of the Executor interface. To stop the threads, we can simply invoke the shutdown method of the
interface as follows:
executor.shutdown;
9.7.2 The Callable Interface
Recall that there are two ways of creating threads. We can either extend the Thread class or implement the Runnable interface. In whichever technique used, we customize
its functionality by overriding the run method. The method has the following signature:
public void run The drawbacks in creating threads this way are:
1. The run method cannot return a result since it has void as its return type. 2. Second, the run method requires you to handle checked exceptions within this method
since the overriden method does not use any throws clause. The Callable interface is basically the same as the Runnable interface without its
drawbacks. To get the result from a Runnable task, we have to use some external means of getting the result. A common technique of which is to use an instance variable for
storing the result. The next code shows how this can be done.
public MyRunnable implements Runnable { private int result = 0;
public void run { ...
result = someValue; }
The result attribute is protected from changes from other codes accessing this class
public int getResult { return result;
Introduction to Programming II Page 137
J.E.D.I.
} }
With the Callable interface, getting the result is simple as shown in the next example. import java.util.concurrent.;
public class MyCallable implements Callable { public Integer call throws java.io.IOException {
... return someValue;
} }
The call method has the following signature: V call throws Exception
V here is a generic type, which means that the return type of call can be of any reference type. You will learn more about generic types in a latter chapter.
There are still more concurrency features in J2SE5.0. Please refer to the API documentation for a detailed discussion of these other features.
Introduction to Programming II Page 138
J.E.D.I.
10 Networking
Java allows you to easily develop applications that perform a variety of tasks over a network. This is one of Javas strength since it was created with the Internet in mind.
Before learning about Java networking, you will first be introduced to some basic concepts on networking.
After completing this lesson, you should be able to: 1. Explain basic networking concepts
• IP address • protocol
• ports • clientserver paradigm
• sockets
2. Create applications using the java networking package • ServerSocket
• Socket • MulticastSocket
• DatagramPacket
10.1 Basic Concepts on Networking