DEV Community

A N M Bazlur Rahman
A N M Bazlur Rahman

Posted on • Originally published at bazlur.com on

Java Thread Programming: Lesson 1

When we run a java program, we are given an execution environment that runs our code one by one. When we have only one execution environment, things are pretty straightforward. All the code runs the same way we write. However, when we have many things to run simultaneously, this single environment will not help us much. That’s where Thread comes into the picture.

We can write our program in a way; it can do multiple things simultaneously in different threads. Each Thread is a unique execution environment.

Creating a thread is easy. In java, we have a class called Thread that can be instantiated and provided a job to it, and it will run independently. Let’s see an example-

package com.bazlur.threads;

public class Playground {
  public static void main(String[] args) {
    Thread thread1 = new Thread(() -> {
      System.out.println("Running from: " + Thread.currentThread());
    });

    Thread thread2 = new Thread(() -> {
      System.out.println("Running from: " + Thread.currentThread());
    });

    thread1.start();
    thread2.start();
  }
}

Enter fullscreen mode Exit fullscreen mode

When we create a thread, we pass a lambda expression to its argument. In the expression, we put our code that we want to run independently.

In the above code, we have created two threads. Each Thread will run independently and simultaneously. Although we have started two threads one by one, we can never tell which one would run first. That’s the nature of the Thread. They are independent and depend on how the operating system schedules them to run.

So far, we have just seen how to create a thread and how to run them. They are super helpful when we have some independent work to be done. For example- we have to write a web server where multiple clients connect to it. If the server runs with a single thread, then at a time, it will be able only to serve one client. That’s not super useful. The server should handle multiple clients at a time, and that’s the point of having it.

Let’s see in the code-

package com.bazlur.threads;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class Playground {
  public static void main(String[] args) throws IOException {
    var serverSocket = new ServerSocket(4040);
    System.out.println("Started server on port " + 4040);

    while (true) {
      var socket = serverSocket.accept();
      var thread = new Thread(() -> {
        handleClient(socket);
      });
      thread.start();
    }
  }

  private static void handleClient(Socket socket) {
    //here we have code for handling a client
  }
}

Enter fullscreen mode Exit fullscreen mode

In the above code, the server keeps listening from clients, and whenever a client connects, it creates a new thread and passes the job into it. That’s how threads help us.

That’s for today, cheers!!

Top comments (0)