Multithreading - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Multithreading

Description:

Ex: A thread is often associated with reading data and interpreting from network ... States that only one thread can execute the associated method or code at a time ... – PowerPoint PPT presentation

Number of Views:63
Avg rating:3.0/5.0
Slides: 19
Provided by: Knigh7
Category:

less

Transcript and Presenter's Notes

Title: Multithreading


1
Multithreading Networking
  • PJ Dillon
  • CS401

2
Introduction
  • Recall one of the first concepts we discussed
  • A program or method is
  • a sequence of instructions
  • One executes after another
  • Execute on the processor
  • Operates on a set of variables
  • Operating Systems can run multiple programs at a
    time
  • Multitasking
  • Boils down to multiple sequences of instructions
    that need executed
  • Processor switches between each

3
What is a Thread?
  • We term a sequence of instructions a Thread
  • Every program has at least one thread
  • Starts executing main()
  • The Thread class provides means to create other
    threads
  • When started, executes run() method
  • run() method defined in Thread is empty
  • Two ways of structuring a thread
  • Create a subclass of Thread
  • Implement Runnable interface
  • Override run() method in each case
  • Executing a thread
  • In another thread (possibly main())
  • First create an instance of a thread
  • Pass it a runnable object
  • or subclass
  • call start() method
  • Actually creates separate execution space
  • Starts executing appropriate run()

4
Thread states
  • Just as programs start, execute, and terminate,
    threads do the same
  • Occurs within the view of other threads
  • Thread.getState() method returns state
  • New
  • Initial state of a newly created thread
  • Has not been started yet
  • No associated execution space
  • Runnable
  • Actively executing instructions
  • Contending with other threads for processor
  • Blocked, Waiting, Timed-Waiting
  • Waiting for something an event to complete
  • IO operation needs to complete
  • Sleep interval to expire
  • Called wait()
  • Terminated
  • Executed run() to completion

5
Thread Scheduling
  • Most PCs have only a single processor
  • Can only execute one thread at a time
  • Scheduling is the process of deciding which
    thread gets the CPU
  • A topic of most OS courses
  • In Java, each thread is given a priority
  • Integer between MIN_PRIORITY and MAX_PRIORITY
    (1-10)
  • setPriority(), getPriority() methods
  • Higher integers have higher priority
  • Of all the threads in the Runnable state
  • Thread with the highest priority gets the
    processor
  • executes until completion
  • All other threads starve until its done
  • If multiple threads of equal priority exist, one
    is arbitrarily chosen

6
Sharing Memory
  • See ex48.java
  • Whats the minimum value sum could take?
  • The statement, sum, is compiled into multiple
    instructions
  • Value copied to processor register
  • Addition is performed
  • Value is copied back to memory
  • Several threads may copy the same value to a
    register
  • Copy stale value register
  • Write over newer value

7
Producer/Consumer Relationship
  • Large programs often have threads associated with
    different tasks
  • One task produces data needs by the other
  • Could be producing data faster than it can be
    processed
  • Ex A thread is often associated with reading
    data and interpreting from network sockets
  • Could read a request
  • Passes the request to some manager responsible
    for handling it
  • Returns to listening for more socket data
  • ex49.java

8
Synchronizing Tasks
  • We need some tools to be able to do this
  • synchronized keyword
  • Added to method declarations
  • Stand-alone synchronized blocks
  • States that only one thread can execute the
    associated method or code at a time
  • Other threads wait until executing one is done
  • Underlying lock associated with the object
  • wait() method
  • Blocks the calling thread
  • Like an indefinite sleep()
  • waits for corresponding notify() or notifyAll()
    call
  • Must be called in a synchronized block
  • notify()/notifyAll()
  • Wakes up one or all other threads, respectively,
    that previously called wait on the object
  • Lets do ex49b.java

9
Locks and Conditions
  • Synchronized, wait(), and notify() have
    limitations
  • Can be used to implement more complex
    synchronization
  • Java 5.0 adds more advanced API
  • Lock
  • Synchronization object
  • A thread must acquire it before executing code
  • Calls lock() method
  • Only one thread can acquire it at a time
  • All others wait until owner calls
  • Condition
  • Manages threads that are forced to wait for some
    condition to become true
  • Consumer has no data available to consume
  • Producer has no space to put new data
  • Calling thread blocks itself calls await()
  • Signaled by another thread to continue
  • Condition must know what lock is protecting it

10
Concurrency Structures
  • The java.util.concurrent. package contains
    predefined classes for
  • More complex synchronization
  • Collections supporting simultaneous access/update
  • Managing threads
  • See The Java Tutorial and API for more info
  • Take CS1550 to learn more about this

11
Networking
12
Basics
  • The Internet
  • Data is transferred in groups of bytes
  • Called a packet
  • Each connected machine is identified by a unique
    IP address
  • 32-bit number
  • Packets are tagged with IP address of the
    destination machine (and source)
  • Transferred to machine through various networks
  • Ultimately arrive at the NIC card of the
    destination
  • Packet data is passed up from the NIC and OS to a
    waiting application
  • Each machine can be running multiple applications
    that expect packets
  • Each application associates itself with a port
  • A unique 16-bit number
  • Different from any other application on the
    machine
  • Packets are tagged with proper port number

13
Communication Protocols
  • The Internet is not perfect
  • Packets may get dropped, never delivered
  • may arrive out of order
  • May get duplicates
  • Two common protocols to deal with this
  • TCP Transmission Control Protocol
  • Connection oriented
  • Orders data prior to passing it up to application
  • Automatically retransmits lost packets
  • Ignores duplicates
  • Application sees data in the same order it was
    sent
  • UDP User Datagram Protocol
  • Connectionless protocol
  • Provides no guarantees with transmission
  • Packets are independent of each other
  • Well focus on TCP

14
Client/Server Model
  • Connections are formed between two nodes
  • One is designated the server
  • Other the client
  • Establishing a Connection
  • Server application starts
  • Associates itself with a port on the NIC
  • Sits, idle, waiting for clients to connect
  • Client application starts
  • Creates TCP communication channel
  • Sends Initial connection request packet to server
    IP and Port
  • Server accepts connection
  • Opens new communication channel
  • One end of a communication channel is called a
    Socket
  • Composed of an InputStream and OutputStream

15
Client/Server in Java
  • The java.net. package defines the ServerSocket
    and Socket classes for establishing connections
  • Server
  • Create ServerSocket object
  • Can be bound to a port at creation time
  • ServerSocket s new ServerSocket(3459)
  • Call accept() method
  • Waits for and accepts a new connection from a
    client
  • Returns a Socket object represented the
    connection
  • Read/Write from associated streams
  • Client
  • Create Socket
  • Can connect to server at creation time or
  • Call connect() method later
  • Read/Write from associated streams
  • MyServer.java, MyClient.java

16
Handling Multiple Connections
  • Lets see MyServer2.java
  • loops forever to continuously accept new
    connections
  • Theres still a problem with this code
  • Server only accepts one connection at a time
  • Only call accept() again after client closes
    connection
  • Code needs to get back to accept() as soon as
    possible
  • Solution is multithreading
  • One thread accepts all connections
  • Spawns others to process the information on the
    socket
  • MyServer3.java, MyClients.java

17
Application Protocol
  • To do anything useful, the server and client have
    to agree on a protocol
  • A set of common messages is defined
  • Text or byte messages
  • Fields laid out in a defined order
  • Marker between messages
  • All messages are the same byte size
  • Special byte or character marks end of a message
  • When one message is sent, another can be expected
    in return
  • E.g. clients sends username in password to server
  • Server responds with login success or failure
  • MyServer4.java, MyClients2.java

18
Going Beyond
  • Large servers with thousands of clients cant
    create a separate thread for each one
  • Most of the time, the thread is blocked waiting
    for input from the user anyway
  • Each client thread performs the same task
  • An alternative lets a thread monitor a number of
    sockets
  • Wait for data on any one of them
  • Returns a group of sockets that have data
    available
  • Code can respond as needed
  • See java.nio. and java.nio.channels. API for
    more
Write a Comment
User Comments (0)
About PowerShow.com