Threads - PowerPoint PPT Presentation

Loading...

PPT – Threads PowerPoint presentation | free to download - id: 73739d-YWNhM



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

Threads

Description:

Threads ICW Lecture 10 Tom Chothia – PowerPoint PPT presentation

Number of Views:69
Avg rating:3.0/5.0
Slides: 33
Provided by: A87
Learn more at: http://www.cs.bham.ac.uk
Category:
Tags: java | threads

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Threads


1
Threads
  • ICW Lecture 10
  • Tom Chothia

2
Last Time
  • XML
  • JDOM
  • XPATH

3
This Lecture
  • URLs
  • A reminder of Sockets.
  • Threads running processes at the same time on
    the same computer.
  • They can be tricky to use.

4
Uniform Resource Locators
  • Many Internet systems use URLs, e.g.
  • http//www.cs.bham.ac.uk/index.html
  • This URL refers to the file index.html on the
    host www.cs.bham.ac.uk which should be accessed
    using http.

Protocol
FilePath
Host
5
URLs aren't just for HTTP
  • https//www.gmail.com/index.html
  • ftp//ftp.funet.fi/pub/standards/RFC/rfc959.txt
  • rmi//68.45.12.7/process
  • svnssh//svn.cwi.nl/projects

6
More complex URLs
  • http//www.cs.bham.ac.uk3080/index.html
  • ftp//tpc_at_my.ftp.bham.ac.uk/myfiles.txt

Port number
Quiry
Username
http//en.wikipedia.org/wiki/Uniform_Resource_Loca
torSyntax
7
URL syntax
  • The most general URL has the syntax
  • protocol//usernamepassword_at_domainport
  • /filepathname?query_stringanchor
  • All parts are options!

8
URLs in Java
  • The java.net.URL class does URLs in Java e.g.
  • URL myURL new URL (http//www.cnn.com/index.htm
    l) myURL.getHost() myURL.getPort()

9
URLs in Java
  • The URL knows the protocol therefore it can
    connect for you
  • InputStream myURL.openStream()
  • More info at http//java.sun.com/j2se/1.4.2/docs/
    api/java/ net/URL.html

10
Sockets
  • A reminder Code from Lecture 6.
  • Only one connection at a time.
  • For multiple connections you need to use Threads.

11
Threads
  • A thread is a Java process that runs at the same
    time as other processes.
  • Use threads for
  • Speed on Multi-process machines.
  • When you want to handle lots of requests at the
    same time.
  • When it's more natural.

12
Example Chat Server
  • Servers handle the requests from the clients
    concurrently we can use Threads.
  • Define the object that handles the request as
    runnable and define a run method.
  • Declare it as a new Thread.
  • Call the start method on it.

13
Threads and Speed
  • Threads only speed up your program if your
    computer has more than one processor.
  • To find the number of processors
  • Runtime runtime Runtime.getRuntime()
  • int noOfProcessors
  • runtime.availableProcessors()

14
Can value ever go below 0?
  • Class Counter
  • private static int value 1000
  • public static void less (int i)
  • if (iltvalue) valuevalue i

15
What about now?
  • Class Counter extends Thread
  • private static int value 1000
  • public static void less (int i)
  • if (iltvalue) valuevalue i

16
Synchronisation
  • Threads run concurrently
  • Threads share the same data space
  • Threads can interact in unexpected ways e.g.
  • The state of object can only be kept consistent
    by guarding against such interactions.

17
What must we do to cope with this?
  • Parts of the program must declare that they must
    have exclusive access to an object whilst
    performing an operation
  • synchronised is used to mark methods or
    statements as needing exclusive access
  • This implements a lock on the object

18
Synchronised methods
  • Use synchronized to declare critical method
  • Only one thread can executing a synchronised
    method on an object at a time
  • To execute a synchronised method the thread must
    obtain the lock on that object
  • Whilst it holds the lock, no other thread can
    execute a synchronised method on that object - it
    is blocked

19
What about now?
  • Class Counter extends Thread
  • private static int value 1000
  • public static synchronised void less (int
    i)
  • if (iltvalue) valuevalue i

20
Synchronised Statements
  • Individual statements can also be synchronised.
  • Syntax
  • synchronized (object) statement
  • e.g
  • ...
  • synchronized (foo)
  • ifoo.getSize()
  • foo.setSize(i2)
  • ...

21
Deadlock
  • Deadlock describes a state where the program
    cannot proceed because of interactions between
    locks held by threads.
  • For instance
  • A is blocked waiting for a lock held by B
  • B is blocked waiting for a lock held by A
  • The interactions can be indirect

22
Synchronisation/locks
  • It is sometimes necessary to restrict access to
    objects by multiple threads - to maintain
    consistency
  • As a rule you should minimise the use of locks
  • Problems can arise
  • not making code thread-safe
  • deadlocks
  • Such problems are hard to debug

23
Sleep
  • A thread can choice to stop running for a time
    using the sleep method
  • Thread.sleep(1000)
  • Pauses the current Thread for about 1 sec.

24
Wait, Notify
  • The wait() method cause the process to stop and
    give up its locks.
  • The process remains paused until another process
    calls notify() on the same object.
  • Notifyall() restarts all waiting processes.

25
Communication Between Threads
  • Threads don't return values.
  • Threads often run independenly to offer a
    service.
  • However a thread can write to e.g. a buffer, for
    other threads to read from.

26
Main Program Creates a Buffer
Main Program
Results Buffer
27
Main can read from this buffer
Main Program
Results Buffer
28
Main creates a thread and passes it the buffer
Main Program
Thread 1
Results Buffer
29
Main creates and starts another thread
Main Program
Thread 1
Results Buffer
Thread 2
30
The threads write to the buffer,from which Main
can read
Main Program
Thread 1
Results Buffer
Thread 2
31
Conclusion
  • Threads let you run concurrent processes.
  • Good for multi-core machines.
  • Good for services.
  • Extend Thread/Implenement Runable
  • Define a run() method.

32
Next Time
  • Javascript
About PowerShow.com