18 Threading
18 Threading
Alexandre Bergel
http://bergel.eu
06/11/2017
Roadmap
synchronize: coordinate
@Test
public void test() {
LoggingHandler handler = new LoggingHandler();
Jeronimo server = new Jeronimo(handler);
assertEquals(handler.nbOfRequests(), 0);
server.receiveRequest("http://myserver.com");
assertEquals(handler.nbOfRequests(), 1);
assertEquals(handler.lastRequest(), "http://myserver.com");
}
}
The Jeronimo class
public class Jeronimo {
private RequestHandler requestHandler;
@Override
public void serve(String aRequest) {
Thread thread = new Thread(this);
thread.start();
}
@Override
public void run() {
...
}
...
}
Testing the creation of threads is not trivial
in most of case
Handing web requests
Web
server
Thread 1
Handing web requests
Web
server
Thread 1
http://localhost:8000/index.html
Handing web requests
Web
server creates
Thread 1
http://localhost:8000/index.html
Request
Thread 2
Handing web requests
Web
server
Thread 1
http://localhost:8000/index.html
Request
Thread 2
Handing web requests
http://localhost:8000/index.html
Web
server
Thread 1
http://localhost:8000/index.html
Request
Thread 2
Jeronimo v1 - Adding a bit of
networking
@Test
public void test() {
LoggingHandler handler = new LoggingHandler();
Jeronimo server = new Jeronimo(handler);
assertEquals(handler.nbOfRequests(), 0);
server.receiveRequest(new Socket());
assertEquals(handler.nbOfRequests(), 1);
}
...
}
public class Jeronimo {
private RequestHandler requestHandler;
private boolean running;
private final int port = 8000;
private ServerSocket serverSocket;
public Jeronimo() {
this(new ThreadedHandler());
}
@Override
public void serve(Socket aSocket) {
socket = aSocket;
Thread thread = new Thread(this);
thread.start();
}
@Override
public void run() {
...
}
}
Creating a Thread
Steps
Create an object of type Runnable & bind it to a new Thread object
Start it
then invokes the run() method of the Runnable object in the new
thread
Threads operations
construction
usually done by passing a runnable object to the thread on
construction
starting
Invoking a thread’s start() method cases the run() method of the
runnable object to run
priority
Threads can be run at different priority levels
Thread life cycle
just created
New
stop()
start()
Terminated
under execution death
Runnable
Waiting
waiting for
another thread waiting for
Timed_
waiting a monitor
Blocked
waiting for
another thread
up to a given time
http://docs.oracle.com/javase/8/docs/api/java/lang/
Thread.State.html
Issues with threads
Scheduling
if # of threads != # of cores (in the CPU), scheduling of threads is
an issue
syncmeth.html
Second, when a synchronized method exits, it automatically
establishes a happens-before relationship with any subsequent
invocation of a synchronized method for the same object. This
guarantees that changes to the state of the object are visible to all
threads.
It is often the case that one need to wait for all the
threads.