Title: Multithreading
1Multithreading
- Threads Concept
- Creating Threads by Extending the Thread class
- Creating Threads by Implementing the Runnable
Interface - Controlling Threads and Thread Status
- Thread Groups
- Synchronization
- Creating Threads for Applets
- Case Studies
2Threads Concept
Multiple threads on multiple CPUs
Multiple threads sharing a single CPU
3Creating Threads by Extending the Thread class
4Example 13.1Using the Thread Class to Create and
Launch Threads
- Objective Create and run three threads
- The first thread prints the letter a 100 times.
- The second thread prints the letter b 100 times.
- The third thread prints the integers 1 through
100.
5Example 13.1Using the Thread Class to Create and
Launch Threads, cont.
TestThread
Run
Click the Run button to access the DOS prompt
then type java TestThread
6Creating Threads by Implementing the Runnable
Interface
7Example 13.2Using the Runnabe Interface to
Create and Launch Threads
- Objective Create and run three threads
- The first thread prints the letter a 100 times.
- The second thread prints the letter b 100 times.
- The third thread prints the integers 1 through
100.
Run
Click the Run button to access the DOS prompt
then type java TestRunnable
TestRunnable
8Controlling Threads and Thread States
- void run()
- Invoked by the Java runtime system to execute the
thread. You must override this method and provide
the code you want your thread to execute. - void start()
- Starts the thread, which causes the run() method
to be invoked. Called by the runnable object in
the client class. - static void sleep(long millis)throws
InterruptedException - Puts the runnable object to sleep for a
specifiedtime in milliseconds.
9Thread States
10Synchronization
A shared resource may be corrupted if it is
accessed simultaneously by multiple threads. For
example, two unsynchronized threads accessing the
same bank account causes conflict.
11Example 13.3Showing Resource Conflict
- Objective create and launch 100 threads, each of
which adds a penny to a piggy bank. Assume that
the piggy bank is initially empty.
12Example 13.3, cont
PiggyBankWithoutSync
Run
13The synchronized keyword
- To avoid resource conflicts, Java uses the
keyword synchronized to synchronize method
invocation so that only one thread can be in a
method at a time. To correct the data-corruption
problem in Example 13.3, you can rewrite the
program as follows
PiggyBankWithSync
Run
14Web Server example I
- A server class starts max number threads of
request_handler that extends Thread class.
hconfig new httpd_config("httpd.conf")
if(!hconfig.isValid()) System.out.println("
Configuration file not correct") return
mime new mime_config(hconfig.getValue("TypesC
onfig")) mime.setDefault(hconfig.getValue("De
faultType")) s new server(hconfig,mime)
if(s.isReady()) s.run()
Server.run
for(i0 iltmaxthreadsi) request_handler
server_thread new request_handler(ss,i,hc,mc,l)
server_thread.start()
15Request_handler.run
Request_handler.reset
public void run() while(true)
reset() s get_socket()
if(snull) // got an error, //
return and kill thread return
set_streams() get_request()
process_request() send_reply()
close_socket()
private void reset() env.reset()
r_method null r_page
null r_http_version null r_address
"" r_agent "-" r_accept
null r_referer "-" r_ctype
null r_clength "0"
r_command "" error_code 0
script false filename
null scriptname null filelength
0 filestream null
16Request_handler.get_socket
private synchronized Socket get_socket()
try Socket incoming ss.accept()
return(incoming) catch(IOException
e) System.out.println(e)
return(null)
17Web Server example II
- A main server class waits for connections and
creates new thread when a new request arrives. - The ServerHandler extends thread class.
while( true )
Socket socket sSocket.accept()
new ServerHandler( socket, numThreadsCreated,
srmProp
).start()
ServerHandler.run
BufferedReader in new BufferedReader(
new InputStreamReader(
socket.getInputStream()))
request.readRequest(in) response
new HResponse(request,socket.getOutputStrea
m()) socket.close()