Please enable JavaScript.
Coggle requires JavaScript to display documents.
NETWORKING AND THREADS IN JAVA, Concurrency - Coggle Diagram
NETWORKING AND THREADS IN JAVA
Networking
Client
and
server applications
communicate over
Socket connections
Socket
represents a
connection between two applications
that may (or may not) be running on two different physical machines
A client must know the
IP Address
(or domain name) and
TCP port number
of the server application
TCP port numbers
16-bit unsigned number
assigned to a
specific server application
allow
different clients
to
connect to the same machine
but
communicate with different applications
running on that machine
port numbers
from
0 to 1023
are reserved for ‘
well known services
’ including
HTTP
,
FTP
,
SMTP
, etc.
A client connects to a server by making a
Server socket
.
Socket s = new Socket("127.0.0.1",4242);
a client can get
input and output streams
from the socket
sock.getInputStream()
these are
low-level ‘connection’ streams
Servers
use a
ServerSocket
that
waits for client requests on a particular port number
.
ServerSocket ‘accepts’ the request
by making
a Socket Connection with the client
Reading text data from the server
Create a
BufferedReader
, chained to an
InputStreamReader
, which is chained to the
input stream from the Socket
InputStreamReader
‘bridge’ stream
that
takes in bytes
and
converts them to text (character) data
used primarily to
act as the middle chain between the high-level BufferedReader
and the
low-level socket input stream
Writing data to the server
Create a
PrintWriter
chained directly to the
Socket’s output stream
.
Call
the
print() or println() methods
to send Strings to the server
.
Threads
thread
(lowercase t)
a
separate thread of execution
in Java
every thread has its own
call stack
Launching a
new thread
pass a
Runnable
to the
Thread’s constructor
NEW
state
a Thread object has been
instantiated
but
start() method has not yet been called
starting a thread
creates a
new stack with the Runnable's run method at the bottom of the stack
RUNNABLE
state
RUNNING
state
the
JVMs thread scheduler
has
selected
the thread to be the
currently-running
(to)
BLOCKED
state
thread is
waiting for data from a stream
,
gone to sleep
, or
waiting for an object's lock
Thread
(capital T)
the
java.lang.Thread class
Thread object
a
thread of execution
needs a
job
to do
an
instance
of something that
implements the Runnable interface
run()
single method
of
Runnable Interface
it goes on the
bottom
of the
new call stack
Thread scheduling
does not work in any particular way
influence turn-taking
by putting your threads to
sleep
periodically
Synchronization
a
tool
that
prevents
:
Thread Interference
when
multiple threads access shared data
Memory Consistency Errors
errors
that result from
inconsistent views of shared memory
can cause
thread contention
two or more threads try to
access the same resource simultaneously
and cause Java runtime to
execute one or more threads more slowly
, or
suspend their execution
Forms
Starvation
Livelock
Deadlock
two or more threads
are
blocked forever
, waiting for each other
Concurrency
the Java platform supports
concurrent programming
basic units of execution:
processes
and
threads