Almighty Bus Error

Loading search...

Threading in Java

A thread is a lightweight process that will act as the child of the process that created it. If the parent process is interrupted the child will be inherited by the kernel process, however, most of the times when the parent dies and child loses its purpose.
A thread will seemingly run at the same time as the other processes which raises concurrency issues but this won’t be discussed in this article.

Threading in a Java implementation can be made by two ways: extending the Thread object and implementing the Runnable interface.

To create a thread in either way it is needed to implement the run() method. Run will be the entry execution point when a thread is created.

Extending Thread object

public class DummyThreadable extends Thread
{
    DummyThreadable() { ; } //dummy constructor.
    public void Run()
    {
        //do some threaded stuff.
    }
}

To create a thread and run it use the start method like the following:

new DummyThreadable().start();

Implementing Runnable interface

public class DummyThreadable implements Runnable
{
    DummyThreadable() { ; } //dummy constructor.
    public void Run()
    {
        //do some threaded stuff.
    }
}

To create a thread using a class that implements Runnable use the following example:

DummyThreadable t = new DummyThreadable();
new Thread(t).start();

A very useful method for thread programing is the sleep method. It receives a time in milliseconds (and optionally nanoseconds). It makes a thread stop its execution flow for the defined time.

long ms = 9001;
int ns = 901;
Thread.sleep(ms, ns);

Thank you for reading!

Tags:  Threading Java