Making a connection — Chapter 15

There are three things we have to learn to get the client working

  1. How to establish the initial connection between the client and server
  2. How to send messages from the client
  3. How to receive messages from the server
  1. who it is (IP address)
  2. which port its running on (TCP port)

TCP ports

The TCP port numbers range from 0 to 1023 — are reserved for well known services. Don’t use them for your own server programs.

Few more import points from the chapter

  1. Client and server applications communicate over a Socket connection.
  2. A Socket represents a connection between two applications which may (or may not) be running on two different physical machines.
  3. A client must know the IP address (or domain name) and the tcp port number of the server application.
  4. A tcp port is a 16-bit unsigned number assigned to a specific server application. TCP port numbers allow different clients to connect to the same machine but communicate with different applications running on that machine.
  5. The port numbers from 0 through 1023 are reserved for well-known services including HTTP, FTP, SMTP, etc.
  6. A client connects to a server by making a Server socket


A thread is a separate thread of execution (a separate call slack) It is a Java class that represents a thread.

How to launch a new thread

  1. Make a Runnable object


Runnable is to a Thread what a job is to a worker. A Runnable is the job a thread is supposed to run. A Runnable holds the method that goes on the bottom of the new thread’s stack (run()). Once the thread becomes runnable, it can move back and forth between runnable, running, and an additional state: temporarily not runnable (also known as ‘blocked’).

The Thread Scheduler

It makes all the decisions about who moves from runnable to running. And about when (and under what circumstances) a thread leaves the running state. The scheduler decides who runs, and for how long, and where the threads go when the scheduler decides to kick them out of the currently running state.

Few more points about threads

  1. A thread with a lower-case “t” is a separate thread of execution in Java.
  2. Every thread In Java has its own call stack.
  3. A Thread with a capital “T” is the java.lang.Thread class. A Thread object represents a thread of execution.
  4. A Thread needs a job to do. A Thread’s job is an instance of something that implements the Runnable interface.
  5. The Runnable interface has just a single method, run(). This is the method that goes on the bottom of the new call stack. In other words. it is the first method to run in the new thread.
  6. To launch a new thread, you need a Runnable to pass to the Thread’s constructor.
  7. A thread is in the NEW state when you have instantiated a Thread object but have not yet called start().
  8. When you start a thread (by calling the Thread object’s start() method), a new stack is created, with the Runnable’s run() method on the bottom of the stack. The thread is now in the RUNNABLE state, waiting to be chosen to run.
  9. A thread is said to be RUNNING when the JVM’s thread scheduler has selected it to be the currently running thread. On a single-processor machine, there can be only one currently-running thread.
  10. Sometimes a thread can be moved from the RUNNING state to a BLOCKED (temporarily non-runnable) state. A thread might be blocked because it’s waiting for data from a stream. Or because it has gone to sleep, or because it is waiting for an object’s lock.
  11. Thread scheduling is not guaranteed to work in any particular way, so you cannot be certain that threads will take turns nicely. You can help influence turn taking by putting your threads to sleep periodically.

Few more things about threads

  1. The static Thread.sleep() method forces a thread to leave the running state for atleast the duration passed to the sleep method. Thread.sleep(200) //puts a thread to sleep for 200 milliseconds.
  2. The sleep() method throws a checked exception (InterruptedException), so all calls to sleep must be wrapped in a try/catch. or declared.
  3. You can use sleep() to help make sure all threads get a chance to run, although there’s no guarantee that when a thread wakes up it’ll go to the end of the runnable line. It might for example, go right back to the front. In most cases, appropriately-timed sleep() calls are all you need to keep your threads Switching nicely.
  4. You can name a thread using the (yet another surprise) setNameO method. All threads get a default name, but giving them an explicit name can help you keep track of threads. especially if you’re debugging with print statements.
  5. You can have serious problems with threads if two or more threads have access to the same object on the heap.
  6. Two or more threads accessing the same object can lead to data corruption if one thread, for example, leaves the running state while still in the middle of manipulating an object’s critical state. • To make your objects thread-safe, decide which statements should be treated as one atomic process (before another thread enters the same method on the same object)
  7. Use the keyword synchronized to modify a method declaration, when you want to prevent two threads from entering that method.
  8. Every object has a single lock, with a single key for that lock. Most of the time we don’t care about that lock; locks come into play only when an object has synchronized methods.
  9. When a thread attempts to enter a synchronized method, the thread must get the key for that object, if it is not available, the thread goes into a kind of waiting lounge. until the key becomes available.
  10. Even if an object has more than one synchronized method, there is still only one key. Once any thread has entered asynchronized method on that object, no thread can enter any other synchronized method on the same object This restriction lets you protect your data by synchronizing any method that manipulates the data.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store