Java Threading - Introduction to Multi-Threading

April 17, 2018 Oclemy Java Threading 2 minutes, 39 seconds

A thread is a path of execution that exists within a process.

One process may have one or more threads.

A Single-threaded process has one thread while a multi-threaded process has more than one threads.

In the single-threaded process, we have only one flow of execution of instructions while in multi-threaded process we have we have multiple sets of instructions executed simultaneously.

A multi-threaded process has parts running concurrently and each part can do a given task independently.

So multi-threadig is a mechanism that allows us write programs in a way in which multiple activities can proceed concurrently in the same program.

But, it is also important to note that in reality, a system that has only a single execution core can create the illusion of concurrent execution. It does this by executing the various threads in an interleaved manner. But it does it fast such that we think that it's actually doing tasks concurrently.

The Main Thread

After you've written your program, compiled it and run it, one thread will be created and associated to the program's main() method.

The thread will execute the statements you'd written in the main() method one after the other.

The main() method is normally the entry point to all java applications. So the thread associated with it is normally called the main thread.

This thread, the main thread, is never created by us explicitly. However, we can manipulate some of its properties.

To do that we need a reference of the Thread class corresponding to that main thread.

So we can use the static currentThread() method defined in the Thread class.

The currentThread() method will return a reference to the currently executing thread object.

Thread myThread = Thread.currentThread();

If we call this in the main() method we will get a reference to the main thread.

public class MrMain {

    public static void main(String[] args) {
        Thread mainThread=Thread.currentThread();
        System.out.println("Current Thread: "+mainThread);
        System.out.println("Current Thread Name: "+mainThread.getName());
        System.out.println("Current Thread ID: "+mainThread.getId());
        System.out.println("Current Thread Priority: "+mainThread.getPriority());
        System.out.println("Current Thread State: "+mainThread.getState());

        System.out.println("Change Thread name and Priority.");

        System.out.println("After the Changes.");
        System.out.println("Current Thread: "+mainThread);
        System.out.println("Current Thread Name: "+mainThread.getName());
        System.out.println("Current Thread Priority: "+mainThread.getPriority());



Current Thread: Thread[main,5,main]
Current Thread Name: main
Current Thread ID: 1
Current Thread Priority: 5
Current Thread State: RUNNABLE
Change Thread name and Priority.
After the Changes.
Current Thread: Thread[EngineThread,7,main]
Current Thread Name: EngineThread
Current Thread Priority: 7

We've printed the thread's name,id,priority and state above.Then modified the name and priority as they are modifiable.

This line Current Thread: Thread[main,5,main] tells us the thread's name,it's priority and the thread group's name.

Whenever a thread is created, a name is automatically generated for it and assigned to it. The name is used to identify the thread.

This name can be changed by explicitly. In fact multiple threads can share the same name since the name is just a string.