Thread Example with its description

suggest change

While launching an application firstly main thread is executed. This Main thread handles all the UI concept of application. If we want to run a long task which does not need the UI, then we use a thread for running that task in the background.

Here is the example of Thread which describes below:

new Thread(new Runnable() {
    public void run() {
        for(int i = 1; i < 5;i++) {  
            System.out.println(i);  
        }
    }
}).start();

We can create a thread by creating a Thread object. start() calls run(). Calling run() yourself does not start a new thread.

We can also run multiple threads independently, which is known as multithreading. A thread can sleep, which makes the currently executing thread sleep (temporarily cease execution) for the specified amount of time. sleep throws InterruptedException, so we have to handle it with try/catch like this.

try{Thread.sleep(500);}catch(InterruptedException e){System.out.println(e);}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents