Junior — Middle
How can a new thread be launched in a program?
sobes.tech AI
Answer from AI
In Android, to start a new thread of execution, you can use the Thread class or more modern tools such as Executors or Kotlin Coroutines.
Example of creating and starting a new thread using Thread in Java:
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
// Code to be executed in the new thread
System.out.println("New thread started");
}
});
thread.start();
It is important to call the start() method, not run(), so that the thread actually runs in parallel.
In Kotlin, coroutines can be used for more convenient management of asynchronous tasks and threads.