Chapter 15

Harshana Samarasinghe
CodeX
Published in
4 min readJun 13, 2022

Networking and Threads Networking and Threads

-Connecting, Sending, and Receiving-

We need a socket connection to join two computers together. A socket is a network object that represents a connection between two computers. The class is Java.net.socket. To build a socket connection, we need to know two things: the IP address, which tells us who we’re talking to, and the TCP port numbers, which tell us which port we’re talking to.

1. The client establishes a socket connection with the server.

2. The client communicates with the server.

3. The client receives a message from the server.

The TCP port number is a 16-bit number that identifies a certain server software. Some critical services use TCP ports 0 through 1023. As a chat server, we can use any port number between 1024 and 65535.

To read data from a socket.

1.Make a Socket connection to the server

2.Make an Input Stream Reader chained to the Socket’s low-level (connection) input stream.

3.Make a Buffered Reader and read!

Input stream reader acts like a bridge which takes in bytes and converts them into text data.

To write data to a socket.

1.Make a Socket connection to the server

2.Make a Print Writer chained to the Socket’s low-level (connection) output stream

3.Write (print) something

Writing a simple server

-Working Process-

Multithreading in Java

Multiple threading is embedded into the structure of the Java language. It’s also simple to start a new execution thread.

Thread t = new Thread();

t.start();

Except for one problem.

Because the thread doesn’t actually perform anything, it “dies” almost as soon as it is created. When a thread dies, the new stack it created vanishes. The narrative comes to a close.

Runnable to a thread denotes the possibility of completing a task. The work that a thread is expected to do is called a runnable. The methods that go at the bottom of the new thread stack are stored in Runnable.

The thread scheduler’s job is to decide which threads should run and which should not. After then, one thread will be allowed to operate while the other is starved.

Other threads can get a chance to run if a thread is put to sleep. When the thread returns to a runnable state, the thread scheduler will pick it to run again.

The synchronized keyword indicates that a key is required for a thread to access the synchronized code.

A lock and a key are both present in a Java object. Objects are frequently unlocked without anyone being aware of it. If an object has synchronized methods, a thread can only access one of those methods if the object’s key is accessible. It will not function if the key has been seized by another thread.

The set name method may be used to name a thread.

Data corruption occurs when two or more threads access the same item. This may be avoided by using synchronized keywords.

There is only one key, even if an object contains several synchronized methods. No thread can enter another synchronized method on the same object once it has entered a synchronized method on it. This limitation allows you to safeguard your data by synchronizing any data manipulation methods.

--

--

Harshana Samarasinghe
CodeX
Writer for

“Truth can only be found in one place: the code.” - Associate Engineer — Java Technology at Virtusa Sri Lanka -