Page 371 - CITS - Computer Software Application -TT
P. 371

COMPUTER SOFTWARE APPLICATION - CITS




           Handling Thread Exceptions and Terminating Threads
           When dealing with exceptions in life cycle of a thread java, there are several ways to handle them.
           •  The most common way is wrapping the code in a try-catch block.
           •  Moreover, when a thread is no longer needed, it should be terminated for the application to avoid memory
              leaks and other issues. To do this, invoke the Thread’s interrupt() or stop() methods. The former method sends
              an interruption signal to the Thread, while the latter stops it immediately and can cause instability in some
              cases.
           Java Threads | How to create a thread in Java
           There are two ways to create a thread:
           1  By extending Thread class

           2  By implementing Runnable interface.
           Thread class
           Thread class provide constructors and methods to create and perform operations on a thread.Thread class
           extends Object class and implements Runnable interface.
           Commonly used Constructors of Thread class
           -  Thread()
           -  Thread(String name)
           -  Thread(Runnable r)
           -  Thread(Runnable r,String name)

           Commonly used methods of Thread class
           1  public void run(): is used to perform action for a thread.
           2  public void start(): starts the execution of the thread.JVM calls the run() method on the 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 int getPriority(): returns the priority of the thread.
           7  public int setPriority(int priority): changes the priority of the thread.
           8  public String getName(): returns the name of the thread.
           9  public void setName(String name): changes the name of the thread.
           10 public Thread currentThread(): returns the reference of currently executing thread.
           11 public int getId(): returns the id of the thread.

           12 public Thread.State getState(): returns the state of the thread.
           13 public boolean isAlive(): tests if the thread is alive.
           14 public void yield(): causes the currently executing thread object to temporarily pause and allow other threads
              to execute.
           15 public void suspend(): is used to suspend the thread(deprecated).
           16 public void resume(): is used to resume the suspended thread(deprecated).
           17 public void stop(): is used to stop the thread(deprecated).

           18 public boolean isDaemon(): tests if the thread is a daemon thread.
           19 public void setDaemon(boolean b): marks the thread as daemon or user thread.



                                                           358

                             CITS : IT&ITES - Computer Software Application - Lesson 101 - 108
   366   367   368   369   370   371   372   373   374   375   376