SEMrush

2 Ways to create a Thread

How to create a Thread?
           
There are two ways to create a thread.

1.      Extend Thread class.
2.      Implement Runnable interface.

Methods of Thread class:

1.      public void start(): starts the execution of the thread.JVM calls the run() method on the thread.
2.      public void run(): is used to perform action for a thread.
3.      public void sleep(long miliseconds): Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds.
4.      public void join(): waits for a thread to die.
5.      public void join(long miliseconds): waits for a thread to die for the specified miliseconds.
6.      public void stop(): is used to stop the thread(depricated).

7.      public int getPriority(): returns the priority of the thread.
8.      public int setPriority(int priority): changes the priority of the thread.
9.      public String getName(): returns the name of the thread.
10.  public void setName(String name): changes the name of the thread.
11.  public Thread currentThread(): returns the reference of currently executing thread.
12.  public int getId(): returns the id of the thread.
13.  public Thread.State getState(): returns the state of the thread.
14.  public boolean isAlive(): tests if the thread is alive.
15.  public void yield(): causes the currently executing thread object to temporarily pause and allow other threads to execute.
16.  public boolean isDaemon(): tests if the thread is a daemon thread.
17.  public void setDaemon(boolean b): marks the thread as daemon or user thread.
18.  public void interrupt(): interrupts the thread.
19.  public boolean isInterrupted(): tests if the thread has been interrupted.
20.  public static boolean interrupted(): tests if the current thread has been interrupted.


Constructors of Thread class:

1.       Thread()
2.       Thread(String name)
3.       Thread(Runnable r)
4.       Thread(Runnable r,String name)


                                                          <Previous             Next>


Comments