Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to create a thread ?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 130
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: