Thread is basically used to execute something in android. Thread class provides start method to start a new thread as well as we can also use Runnable interface to create some thread.
So there are two way to create a thread that are :
1. By extending Thread class
2. By implementing Runnable interface
1. Example :
public class MyThread extends Thread 
{ 
     public MyThread() 
       { 
         super("MyThread");
        } 
     public void run()
      {
        Thread.sleep(1000);
      } 
}
2. Example
public class AnotherThread implements Runnable{
    @Override
    public void run() {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
Now from main activity :
MyThread thread1 = new MyThread();
thread1.start();
AnotherThread thread2 = new AnotherThread();
thread2.run();
                       
                    
0 Comment(s)