Presentation is loading. Please wait.

Presentation is loading. Please wait.

Multithreading Threads Concept

Similar presentations


Presentation on theme: "Multithreading Threads Concept"— Presentation transcript:

1 Multithreading 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

2 Threads Concept Multiple threads on multiple CPUs
Multiple threads sharing a single CPU

3 Creating Threads by Extending the Thread class

4 Example 13.1 Using 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.

5 Example 13.1 Using the Thread Class to Create and Launch Threads, cont.
TestThread Run Click the Run button to access the DOS prompt; then type java TestThread

6 Creating Threads by Implementing the Runnable Interface

7 Example 13.2 Using 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

8 Controlling 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 specified time in milliseconds.

9 Thread States

10 Synchronization 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.

11 Example 13.3 Showing 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.

12 PiggyBankWithoutSync
Example 13.3, cont PiggyBankWithoutSync Run

13 The 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

14 Web 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("TypesConfig")); mime.setDefault(hconfig.getValue("DefaultType")); s = new server(hconfig,mime); if(s.isReady()){ s.run(); } Server.run for(i=0; i<maxthreads;i++){ request_handler server_thread = new request_handler(ss,i,hc,mc,l); server_thread.start(); }

15 Request_handler.run Request_handler.reset public void run(){ while(true){ reset(); s = get_socket(); if(s==null){ // 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; }

16 Request_handler.get_socket
private synchronized Socket get_socket(){ try{ Socket incoming = ss.accept(); return(incoming); } catch(IOException e){ System.out.println(e); return(null);

17 Web 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.getOutputStream()); socket.close();


Download ppt "Multithreading Threads Concept"

Similar presentations


Ads by Google