Top Banner
Threads ICW Lecture 10 Tom Chothia
32

Threads ICW Lecture 10 Tom Chothia. Last Time XML JDOM XPATH.

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Threads ICW Lecture 10 Tom Chothia. Last Time XML JDOM XPATH.

Threads

ICW Lecture 10

Tom Chothia

Page 2: Threads ICW Lecture 10 Tom Chothia. Last Time XML JDOM XPATH.

Last Time

• XML

• JDOM

• XPATH

Page 3: Threads ICW Lecture 10 Tom Chothia. Last Time XML JDOM XPATH.

This Lecture

• URLs

• A reminder of Sockets.

• Threads: running processes at the same time on the same computer.

• They can be tricky to use.

Page 4: Threads ICW Lecture 10 Tom Chothia. Last Time XML JDOM XPATH.

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 Host FilePath

Page 5: Threads ICW Lecture 10 Tom Chothia. Last Time XML JDOM XPATH.

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

• svn+ssh://svn.cwi.nl/projects

Page 6: Threads ICW Lecture 10 Tom Chothia. Last Time XML JDOM XPATH.

More complex URLs

http://www.cs.bham.ac.uk:3080/index.html

ftp://[email protected]/myfiles.txt

http://en.wikipedia.org/wiki/Uniform_Resource_Locator#Syntax

Port number

Username Quiry

Page 7: Threads ICW Lecture 10 Tom Chothia. Last Time XML JDOM XPATH.

URL syntax

• The most general URL has the syntax:

protocol://username:password@domain:port

/filepathname?query_string#anchor

• All parts are options!

Page 8: Threads ICW Lecture 10 Tom Chothia. Last Time XML JDOM XPATH.

URLs in Java

The java.net.URL class does URLs in Java e.g.:

URL myURL = new URL (“http://www.cnn.com/index.html); myURL.getHost(); myURL.getPort();

Page 9: Threads ICW Lecture 10 Tom Chothia. Last Time XML JDOM XPATH.

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

Page 10: Threads ICW Lecture 10 Tom Chothia. Last Time XML JDOM XPATH.

Sockets

• A reminder: Code from Lecture 6.

• Only one connection at a time.

• For multiple connections you need to use Threads.

Page 11: Threads ICW Lecture 10 Tom Chothia. Last Time XML JDOM XPATH.

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”.

Page 12: Threads ICW Lecture 10 Tom Chothia. Last Time XML JDOM XPATH.

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.

Page 13: Threads ICW Lecture 10 Tom Chothia. Last Time XML JDOM XPATH.

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();

Page 14: Threads ICW Lecture 10 Tom Chothia. Last Time XML JDOM XPATH.

Can “value” ever go below 0?

Class Counter {

private static int value = 1000;

public static void less (int i) { if (i<value) {value=value – i; } }}

Page 15: Threads ICW Lecture 10 Tom Chothia. Last Time XML JDOM XPATH.

What about now?

Class Counter extends Thread {

private static int value = 1000;

public static void less (int i) { if (i<value) {value=value – i; }

} }

Page 16: Threads ICW Lecture 10 Tom Chothia. Last Time XML JDOM XPATH.

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.

Page 17: Threads ICW Lecture 10 Tom Chothia. Last Time XML JDOM XPATH.

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

Page 18: Threads ICW Lecture 10 Tom Chothia. Last Time XML JDOM XPATH.

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

Page 19: Threads ICW Lecture 10 Tom Chothia. Last Time XML JDOM XPATH.

What about now?

Class Counter extends Thread {

private static int value = 1000;

public static synchronised void less (int i) {

if (i<value) {value=value – i; }

} }

Page 20: Threads ICW Lecture 10 Tom Chothia. Last Time XML JDOM XPATH.

Synchronised Statements

• Individual statements can also be synchronised.

• Syntax:

synchronized (object) {statement}

e.g:

...;

synchronized (foo) { i=foo.getSize();

foo.setSize(i*2); }

...

Page 21: Threads ICW Lecture 10 Tom Chothia. Last Time XML JDOM XPATH.

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

Page 22: Threads ICW Lecture 10 Tom Chothia. Last Time XML JDOM XPATH.

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

Page 23: Threads ICW Lecture 10 Tom Chothia. Last Time XML JDOM XPATH.

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.

Page 24: Threads ICW Lecture 10 Tom Chothia. Last Time XML JDOM XPATH.

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.

Page 25: Threads ICW Lecture 10 Tom Chothia. Last Time XML JDOM XPATH.

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.

Page 26: Threads ICW Lecture 10 Tom Chothia. Last Time XML JDOM XPATH.

Main Program Creates a Buffer

Main Program

Results Buffer

Page 27: Threads ICW Lecture 10 Tom Chothia. Last Time XML JDOM XPATH.

Main can read from this buffer

Main Program

Results Buffer

Page 28: Threads ICW Lecture 10 Tom Chothia. Last Time XML JDOM XPATH.

Main creates a thread and passes it the buffer

Main Program

Results Buffer

Thread 1

Page 29: Threads ICW Lecture 10 Tom Chothia. Last Time XML JDOM XPATH.

Main creates and starts another thread

Main Program

Results Buffer

Thread 1

Thread 2

Page 30: Threads ICW Lecture 10 Tom Chothia. Last Time XML JDOM XPATH.

The threads write to the buffer,from which Main can read

Main Program

Results Buffer

Thread 1

Thread 2

Page 31: Threads ICW Lecture 10 Tom Chothia. Last Time XML JDOM XPATH.

Conclusion

• Threads let you run concurrent processes.

–Good for multi-core machines.

–Good for services.

• Extend Thread/Implenement Runable

• Define a run() method.

Page 32: Threads ICW Lecture 10 Tom Chothia. Last Time XML JDOM XPATH.

Next Time

• Javascript