Networking, Java threads and synchronization

51
Networking, Java threads and synchronization PRIS lecture 4 Fredrik Kilander

Transcript of Networking, Java threads and synchronization

Page 1: Networking, Java threads and synchronization

Networking, Java threads and synchronization

PRIS lecture 4Fredrik Kilander

Page 2: Networking, Java threads and synchronization

Application

Presentation

Session

Transport

Network

Data link

Physical

OSIApplication

Transport

Internet

TCP/IP

Host-to-network

A. S. Tanenbaum, Computer Networks, 3rd Ed.

Page 3: Networking, Java threads and synchronization

Application

Presentation

Session

Transport

Network

Data link

Physical

OSIApplication

Transport

Internet

TCP/IP

Host-to-network

A. S. Tanenbaum, Computer Networks, 3rd Ed.

Middleware

Page 4: Networking, Java threads and synchronization

SSHBitTorrent SMTP DNSHTTP

TCP UDP

IP

LAN WiFi PPP

Application

Transport

Network

Data link +physical

Protocols

Networks

Adapted from A. S. Tanenbaum, Computer Networks, 3rd Ed.

MANWAN 3G

Page 5: Networking, Java threads and synchronization

Application

Transport

Network

SOAP

XML

HTTP

Application

Transport

Network

SOAP

XML

HTTP

Network

Waves

Remote Procedure Call

Packet

Connection

Connection

Message

Message

Data link

Physical

Data link

Physical

Data link

Physical

Frame

Page 6: Networking, Java threads and synchronization

CommunicationLayers, interfaces, and protocols in the OSI (Open Systems Interconnection)

reference model.• Divided into 7 layers each deals with one specific aspects of the communication

Page 7: Networking, Java threads and synchronization

Transport

Network

Data link

Physical

unreliable

unreliable

reliable

reliable

Transport

Network

Data link

Physical

Page 8: Networking, Java threads and synchronization

Reliable data transmission

• Divide data into packets• Add error-detection/correction to payload• Add sequence numbers• Add a timer for each packet sent• Keep resending packets until they are ack’d.• Acknowledge received packets

Page 9: Networking, Java threads and synchronization

Transport

Physical

Transport

Physical

Network

Data link

Network

Data link

Point-to-point connection

Wire or EM beam

Page 10: Networking, Java threads and synchronization

Transport

Physical

Transport

Physical

Network

Data link

Network

Data link

Common media, no longer point-to-point

Transport

Physical

Network

Data link

Coaxial cable or radio channel(wave propagation medium)

Page 11: Networking, Java threads and synchronization

MAC

Application

Presentation

Session

Transport

Network

Data link

Physical

OSI

Medium ACcess Layer

Page 12: Networking, Java threads and synchronization

Transport

Physical

Transport

Physical

Network

Data link

Network

Data link

MAC layer negotiates access to shared medium

Transport

Physical

Network

Data link

MAC MAC MAC

Page 13: Networking, Java threads and synchronization

Ethernet history – thick cable

Page 14: Networking, Java threads and synchronization

Ethernet history – thinwire

Page 15: Networking, Java threads and synchronization

Ethernet history – CAT6/WiFi

Page 16: Networking, Java threads and synchronization

Ada Bea Cia Didi Eve

MAC MAC MAC MAC MAC

Stations share the broadcast medium

Only one station may send - all listen

Transmissions are addressed • to an interface (unicast)• or to a group (multicast)• or to all (broadcast)

Page 17: Networking, Java threads and synchronization

Ada Bea Cia Didi Eve

MAC MAC MAC MAC MAC

Stations share the broadcast medium

The MAC-address depends on the medium

Ethernet 48 bits vv:vv:vv:ss:ss:ssBlueTooth 48 bits NAP(16)UAP(8)LAP(24)

NAP:Non-significant address portionLAP:Lower address portionUAP:Upper address portion

00:1F:3B:BF:CA:35

Page 18: Networking, Java threads and synchronization

Ada Bea Cia Didi Eve

MAC MAC MAC MAC MAC

Stations share the broadcast medium

Simultaneous broadcasts leads tocollisions

Page 19: Networking, Java threads and synchronization

Ada Bea Cia Didi Eve

MAC MAC MAC MAC MAC

Stations share the broadcast medium

Simultaneous broadcasts leads tocollisions

Interference where frames overlap

Page 20: Networking, Java threads and synchronization

Application

Presentation

Session

Transport

Network

Data link

Physical

OSIApplication

Transport

Internet

TCP/IP

Host-to-network

A. S. Tanenbaum, Computer Networks, 3rd Ed.

Page 21: Networking, Java threads and synchronization

Data network: routers and links

Page 22: Networking, Java threads and synchronization

Network/Internet layer – routing of packets

Packets may be• Lost (router memory is full)• Reordered (go separate paths)• Delayed (queued)

Each router selects the best output line for the packet

Page 23: Networking, Java threads and synchronization

Application

Presentation

Session

Transport

Network

Data link

Physical

OSIApplication

Transport

Internet

TCP/IP

Host-to-network

A. S. Tanenbaum, Computer Networks, 3rd Ed.

Page 24: Networking, Java threads and synchronization

Transport layer – virtual connection

Endpoints simulate a continuous stream of bytes

TCP: packet sizes depend on delayFixes lost and reordered packets

Page 25: Networking, Java threads and synchronization

Multithreading

Concurrent computing

Page 26: Networking, Java threads and synchronization

Threads allow parallel execution in one pgm

Threading is a programming language construct

Threads

Page 27: Networking, Java threads and synchronization

Threads allow parallel execution in one pgm

Threads

The main thread is issued by the operatingsystem.

It enters the main routine of the program.

When it exits the program ends.

Page 28: Networking, Java threads and synchronization

Threads allow parallel execution in one pgm

Threads

Other threads are issued from the main thread.

They enter other routines of the same program.

Several threads can enter the same routine.

When the entry point is exited, the thread ends.

Page 29: Networking, Java threads and synchronization

Threads allow parallel execution in one pgm

Threads

The main thread always enters the main program.

int foo() {a = b;c = d;...

}

void bar (int x) {x = u * b;...

}

void main (String argv[]) {boolean o;double p;...

}

Other threads execute with other routinesas their main programs.

Page 30: Networking, Java threads and synchronization

Threads allow parallel execution in one pgm

Threads

All threads shareglobal variables.

int a = 0;boolean b;

void foo() {int a;...

}

void bar (int x) {float y;...

}

void main (String argv[]) {

a = 42;...

}

Threads can be implementedin several ways:

Virtual threads by interpreter

Parallel processes from OS

Threads supported by OS

Threads can be implementedin several ways:

Virtual threads by interpreter

Parallel processes from OS

Threads supported by OS

Threads do notshare local vari-ables becauseeach thread has its own stack.

Page 31: Networking, Java threads and synchronization

Mental models

• Single-threaded – whole program• Multi-threaded – several small programs

• Where and how can and should threads interact in my program?

Page 32: Networking, Java threads and synchronization

Surreptious multi-threading:

• Callback APIs– GUI events– Messaging systems– Discovery systems

• Timers• RPC server

Page 33: Networking, Java threads and synchronization

Race condition

Insert presentation here

Page 34: Networking, Java threads and synchronization

Threads in Java

A class that supports threading implementsinterface Runnable.

import java.lang.Runnable;import java.lang.Thread;

class MyServer implements Runnable {

// In interface Runnable:public void run () {...

}

public void main (String argv[]) {...new Thread (this).start ();...

}}

Interface Runnable hasone method: run(). Thisis a thread’s entry point.

A new thread is createdjust like any other object.It is given the objectwhere to execute andtold to start running.

Page 35: Networking, Java threads and synchronization

Threads in Java

A class that supports threading implementsinterface Runnable.

Interface Runnable hasone method: run(). Thisis a thread’s entry point.

A new thread is createdjust like any other object.It is given the objectwhere to execute andtold to start running.

class MyServer implements Runnable...Thread t = new Thread (this);...public void run() {...}

class MyServer implements Runnable...Thread t = new Thread (this);...public void run() {...}

Thread t

public void start()

Thread t

public void start()

Page 36: Networking, Java threads and synchronization

Threads in Java

class MyServer...Thread t = new Thread (new Worker());...t.start ();

class MyServer...Thread t = new Thread (new Worker());...t.start ();

Thread t

public void start()

Thread t

public void start()

class Worker implements Runnable

public void run() {...}

class Worker implements Runnable

public void run() {...}

Page 37: Networking, Java threads and synchronization

Threads in JavaIn Java you must use method Runnable.run(). Where is the diversity?

import java.lang.Runnable;import java.lang.Thread;

class MyServer implements Runnable {int h;

// In interface Runnable:public void run () {switch (h) {case 1: foo(); break;case 2: bar(); break;}

}

public void main (String argv[]) {...h = 1;new Thread (this).start ();h = 2;

}}

Programmaticor computedchoice.

Race conditionon variable h!

Race conditionon variable h!

Page 38: Networking, Java threads and synchronization

Threads in JavaIn Java you must use method Runnable.run(). Where is the diversity?

import java.lang.Runnable;import java.lang.Thread;

class MyServer {

public void main (String argv[]) {new Thread (new Foo ()).start ();new Thread (new Bar ()).start ();

}}

Separate outmethods intotheir own classes.

import java.lang.Runnable;class Foo implements Runnable {

public void run () {...

}}

import java.lang.Runnable;class Bar implements Runnable {

public void run () {...

}}

Page 39: Networking, Java threads and synchronization

Threads in Java

import java.lang.Runnable;import java.lang.Thread;

class MyServer {

protected void doWork () {...

}

public void main (String argv[]) {...new Thread (new Runnable () {public void run () {doWork ();

}});

}}

In Java you must use method Runnable.run(). Where is the diversity?

Anonymoussubclassing

Page 40: Networking, Java threads and synchronization

Threads in JavaWait for a thread to die: Thread.join()

import java.lang.Runnable;import java.lang.Thread;

class MyServer {

public void main (String argv[]) {...// Create thread t.Thread t = new Thread (new Foo ()).start ();...// Main thread waits for t to die.t.join ();...

}}

Page 41: Networking, Java threads and synchronization

Threads in JavaThread control

• sleep (long ms) – suspend caller for at least ms milliseconds

• join (Thread t) – suspend caller until other thread t has exited

• wait() – suspend until notified (in a synchronized block)

• notify() – release a wait()ing thread (in a synchronized block)

Page 42: Networking, Java threads and synchronization

Synchronization in JavaThreads that access common variables together can seriously mess up the state of the program.

Synchronization is achieved by monitors.

A monitor is a non-sharable entity associated with a Java object (choosen by the programmer).

Synchronized code is locked by the object.

A thread must have the monitor to be able to execute the synchronized code.

Page 43: Networking, Java threads and synchronization

Synchronization in JavaWhen a method is declared as synchronized the monitor is retrieved from the method’s object. Potential inefficiency.

public synchronized void enqueue() {...}

When a block of code is synchronized, any object’s monitor can be used:

synchronized (myQueue) { ... }

Page 44: Networking, Java threads and synchronization

Synchronization in JavaA thread that attempts to enter a synchronized method or blockmust wait in a queue for the monitor.

When the monitor is released the thread continues to execute.

While the thread is inside the synchronized section it canrelease the monitor and wait().

synchronized (myQueue) { ...wait (); // Nothing to do...}

When some other thread has the monitor it can notify the waitingthread, giving back the monitor and allowing it to continue:

synchronized (myQueue) { ...notify (); // Work is ready for you...}

Page 45: Networking, Java threads and synchronization

Synchronization in Java

Producer threadAdds items to

the queue myQueue

Producer threadAdds items to

the queue myQueuemyQueuemyQueue Consumer thread

Removes items from the queue myQueue

Consumer threadRemoves items from the queue myQueue

head tailmyQueue

Page 46: Networking, Java threads and synchronization

Synchronization in Java

Producer threadAdds items to

the queue myQueue

Producer threadAdds items to

the queue myQueuemyQueuemyQueue Consumer thread

Removes items from the queue myQueue

Consumer threadRemoves items from the queue myQueue

Producer receives myQueue’s monitor

Producer asks for myQueue’s monitor

Producer enqueues items

Producer returns myQueue’s monitor

Consumer dequeues items

Qmobj

Consumer asks for myQueue’s monitor

Consumer receives myQueue’s monitor

Consumer returns myQueue’s monitor

Synchronizedcode

Synchronizedcode

Producer iswaiting for

the monitor

Consumer iswaiting forthe monitor

Page 47: Networking, Java threads and synchronization

Synchronization in Java

Producer threadAdds items to

the queue myQueue

Producer threadAdds items to

the queue myQueuemyQueuemyQueue Consumer thread

Removes items from the queue myQueue

Consumer threadRemoves items from the queue myQueue

Producer receives myQueue’s monitor

Producer asks for myQueue’s monitor

Producer enqueues items

Producer returns myQueue’s monitor

Consumer dequeues items

Qmobj

Consumer asks for myQueue’s monitor

Consumer receives myQueue’s monitor

Consumer returns myQueue’s monitor

Synchronizedcode

Synchronizedcode

Producer iswaiting for

the monitor

Consumer iswaiting forthe monitor

Both threads are waiting forthe monitor:The winner selection is undefined

Both threads are waiting forthe monitor:The winner selection is undefined

Page 48: Networking, Java threads and synchronization

Synchronization in JavaA typical application of the wait()/notify() mechanism is a consumerproducer pair with a queue between them.

Access to the queue must be synchronized.

While the queue is empty the consumer does not execute.

M

myQueue

synchronized (myQueue) {myQueue.enqueue (x);

}synchronized (qmObj) {notify ();

}

// In qmObj:

for (;;) {while (!myQueue.isEmpty()) {

synchronized (myQueue) {e = myQueue.dequeue ();

}}synchronized (this) {

wait ();}

}

Page 49: Networking, Java threads and synchronization

// In qmObj:

for (;;) {while (!myQueue.isEmpty()) {synchronized (myQueue) {e = myQueue.dequeue ();

}}synchronized (this) {wait ();

}}

M

myQueue

synchronized (myQueue) {myQueue.enqueue (x);

}synchronized (qmObj) {

notify ();}

Page 50: Networking, Java threads and synchronization

Stuff we did not mention ...Avoid Thread. stop() suspend() resume() (deprecated)

Class Threadgroup : handle threads as a unit

Class ThreadLocal : thread-specific data

Thread intercommunication with pipes.

Thread priorities.

Page 51: Networking, Java threads and synchronization

The End