6 minutes read

Introduction

When you have already learned the basics of programming in Kotlin and the capabilities of the language, you move on to creating larger and more serious projects to develop your skills. In such projects, you think not only about the correct code execution but also about its effective execution. This is where you need threads, which allow you to execute some actions in your program in parallel instead of sequentially.

In this topic, you will learn what Kotlin threads are and how you can work with them in your code.

Threads in Kotlin

Kotlin was originally designed with built-in multithreading support. Threads are supported on the level of the JVM, the level of the language (special keywords), and the level of the standard library. Every Kotlin program has at least one thread, which is called main; it is created automatically by the JVM process to execute statements inside the main() function. Any Kotlin program has some other default threads as well (for example, a separate thread for the garbage collector).

Throughout all stages of development of the Kotlin language, the approach to multithreading has changed from the use of low-level threads to some high-level abstractions. However, understanding the fundamental concept remains very important for a good developer.

A class for threads

A thread is a sequence of instructions that a program can execute when it runs. Each thread is represented by an object – an instance of the java.lang.Thread class (or its subclass).

As you know, every program creates at least one thread: the main thread, which runs the main() function, and such a program is called a single thread program. If you create one or more threads that will be performed in parallel, it becomes a multithreaded program. In the following topics, you will learn how to create your own thread, which will perform a certain sequence of actions. But now, let's try to look at the thread that executes at the moment.

The Thread class has a static method named currentThread to obtain a reference to the currently executing thread object:

val thread: Thread = Thread.currentThread() // the current thread

The Thread class stores basic information about the thread: its name, identifier (long), priority, and some other characteristics that can be obtained through its functions. Let's study them in more detail and take the thread main as an example.

The information on the main thread

The example below demonstrates how to obtain the characteristics of the main thread by making references to it through an object of the Threadclass.

fun main() {
    val t: Thread = Thread.currentThread() // main thread

    println("Name: ${t.name}")
    println("ID: ${t.id}")
    println("Alive: ${t.isAlive}")
    println("Priority: ${t.priority}")
    println("Daemon: ${t.isDaemon}")
}

All statements in this program are executed by the main thread. You can see the general information about this thread:

  • t.name returns the thread's name;

  • t.id returns the thread's unique identifier;

  • t.isAlive tells us whether the thread has been started and hasn't died yet;

  • t.priority returns the priority of the given thread. Every thread has a priority that determines the order of execution: threads with a higher priority are executed before threads with lower priorities;

  • t.isDaemon checks whether the thread is a daemon. A daemon thread (comes from UNIX terminology) is a low priority thread that runs in the background to perform tasks such as garbage collection and so on. The JVM does not wait for daemon threads to stop before exiting, while it does so for non-daemon threads.

The output of the program:

Name: main
ID: 1
Alive: true
Priority: 5
Daemon: false

Each characteristic can be changed by setting a new value:

val t: Thread = Thread.currentThread() // main thread

t.name = "my-thread"
println("New name: ${t.name}") // New name: my-thread

The same code can be applied to any current thread, not only the main one.

Conclusion

In this topic, you've learned some useful information about threads:

  • A thread is a sequence of instructions that can be performed in parallel.

  • The class Thread allows working with a thread as an object.

  • You can obtain a reference to the currently executing thread using the static method Thread.currentThread().

  • Any thread has a name, an identifier, a priority, as well as isAlive and isDaemon characteristics, which you can change.

80 learners liked this piece of theory. 4 didn't like it. What about you?
Report a typo