Thread Properties




                    1
Objectives

On completion of this period, you would be
able to learn

•   Thread class
•   Constructors of Thread class
•   Methods of Thread class
•   Fields of Thread class
•   Applications of Java threads




                                             2
Recap

• In the previous class we learnt
  • Deadlock problem
  • Threads and deadlock
  • To avoid deadlock in threads




                                    3
Thread Class
• As we know already, Java provides two ways of
  creating threads
  • By extending Thread class
  • By implementing the Runnable interface

• Thread class is defined in java.lang package
• The Thread class provides
  • Constructors
  • Methods
  • Fields
• These are known as Thread properties


                                                  4
Constructors of Thread Class

• Thread()
  • Allocates a new Thread object

• Thread (String name)
  • Creates a new Thread object with the given name

• Thread( Runnable target)
  • Creates a Thread object for a given Runnable Object




                                                          5
Constructors of Thread Class
                                           Contd . . .


• Thread (Runnable target, String name)
  • Creates a Thread object for a given Runnable
    Object and give a name to the thread


• Thread(ThreadGroup group, String name)
  • Creates a Thread object within a group and gives a
    proper name for the thread




                                                         6
Methods of Thread Class
• We have already seen several methods of Thread
  class
• We will look at some more methods
• currentThread()
   • Returns a reference to the currently executing thread
     object
• yield()
   • Causes the runtime to context switch from current
     thread to next available running thread
• sleep(int n)
   • Current thread sleeps for n milliseconds

                                                             7
Methods of Thread Class
                                             Contd . . .
• start()
   • Used to start a thread
   • run() method will be called
• run()
   • Is the body of running thread
   • To be overridden
• stop()
   • Causes the thread to stop immediately
• setName (String name)
   • Sets the name for the current thread

                                                           8
Methods of Thread Class
                                               Contd . . .

• getName()
  • Returns the name of the current thread
• activeCount()
  • Returns the number of active threads in the current
    thread's thread group
• getThreadGroup()
  • Returns the thread group to which this thread belongs
• getState()
  • Returns the state of this thread


                                                             9
Fields of Thread class

• While discussing thread priorities, we looked
  at the constants of Thread
• These are the fields of Thread class, and they
  are
  • Thread.MAX_PRIORITY
  • Thread.NORM_PRIORITY
  • Thread.MIN_PRIORITY




                                                   10
Example Program

// A simple program to demonstrate the currentThread()
    method
// It also uses the setName() method       Use of currentThread()
class CurrentThreadDemo {
  public static void main(String args[]) {
    Thread t = Thread.currentThread();
    System.out.println("Current thread: " + t); of setName()
                                           Use
    // change the name of the thread
    t.setName("My Thread");
    System.out.println("After name change: " + t);


                                                                    11
Example Program
                                                   Contd . . .
 try {
      for(int n = 5; n > 0; n--) {
        System.out.println(n);         Delay One second
        Thread.sleep(1000);                 To be caught when sleep()
      }                                     is called

    } catch (InterruptedException e) {
      System.out.println("Main thread interrupted");
                                            Output
    }
  }
}

                                                                        12
Applications of Java threads

• Monitoring the state of resources
   • Databases , servers etc
• Listening to events
   • GUI, Networking
• Operations that take a while to complete
   • Printing
• Any application that is parallel in nature




                                               13
Summary

• In this class, you have learnt

   •   Thread class
   •   Constructors of Thread class
   •   Methods of Thread class
   •   Fields of Thread class
   •   The applications of Java threads




                                          14
Quiz

1. start() method of Thread, in turn calls

  A.   execute() method
  B.   run() method
  C.   stop() method
  D.   None




                                             15
Quiz Contd..

2. Which of the following methods return a
  reference to the currently executing thread

  A.   this()
  B.   super()
  C.   currentThread()
  D.   No such method




                                                16
Quiz Contd..

3. Which of the following method must be
  overridden to create a user thread

  A.   start() method
  B.   run() method
  C.   stop() method
  D.   None




                                           17
Quiz Contd..

4. Which of the following methods context
  switch to another thread

A. run()
B. sleep()
C. yield()
D. No such method




                                            18
Frequently Asked Questions

1.   List the methods of Thread class
2.   List the different constructors of Thread class
3.   List the fields of Thread class
4.   What are the applications of Java threads ?




                                                       19
Assignment

• Create a java thread, called TimerThread
• TimerThread should
  • Print time continuously, for every second, in the
    following format
      • hh:mm:ss, where
      • hh is hours
      • mm is minutes
      • ss is seconds
• Write a Java application to test the TimerThread

                                                        20

ThreadProperties

  • 1.
  • 2.
    Objectives On completion ofthis period, you would be able to learn • Thread class • Constructors of Thread class • Methods of Thread class • Fields of Thread class • Applications of Java threads 2
  • 3.
    Recap • In theprevious class we learnt • Deadlock problem • Threads and deadlock • To avoid deadlock in threads 3
  • 4.
    Thread Class • Aswe know already, Java provides two ways of creating threads • By extending Thread class • By implementing the Runnable interface • Thread class is defined in java.lang package • The Thread class provides • Constructors • Methods • Fields • These are known as Thread properties 4
  • 5.
    Constructors of ThreadClass • Thread() • Allocates a new Thread object • Thread (String name) • Creates a new Thread object with the given name • Thread( Runnable target) • Creates a Thread object for a given Runnable Object 5
  • 6.
    Constructors of ThreadClass Contd . . . • Thread (Runnable target, String name) • Creates a Thread object for a given Runnable Object and give a name to the thread • Thread(ThreadGroup group, String name) • Creates a Thread object within a group and gives a proper name for the thread 6
  • 7.
    Methods of ThreadClass • We have already seen several methods of Thread class • We will look at some more methods • currentThread() • Returns a reference to the currently executing thread object • yield() • Causes the runtime to context switch from current thread to next available running thread • sleep(int n) • Current thread sleeps for n milliseconds 7
  • 8.
    Methods of ThreadClass Contd . . . • start() • Used to start a thread • run() method will be called • run() • Is the body of running thread • To be overridden • stop() • Causes the thread to stop immediately • setName (String name) • Sets the name for the current thread 8
  • 9.
    Methods of ThreadClass Contd . . . • getName() • Returns the name of the current thread • activeCount() • Returns the number of active threads in the current thread's thread group • getThreadGroup() • Returns the thread group to which this thread belongs • getState() • Returns the state of this thread 9
  • 10.
    Fields of Threadclass • While discussing thread priorities, we looked at the constants of Thread • These are the fields of Thread class, and they are • Thread.MAX_PRIORITY • Thread.NORM_PRIORITY • Thread.MIN_PRIORITY 10
  • 11.
    Example Program // Asimple program to demonstrate the currentThread() method // It also uses the setName() method Use of currentThread() class CurrentThreadDemo { public static void main(String args[]) { Thread t = Thread.currentThread(); System.out.println("Current thread: " + t); of setName() Use // change the name of the thread t.setName("My Thread"); System.out.println("After name change: " + t); 11
  • 12.
    Example Program Contd . . . try { for(int n = 5; n > 0; n--) { System.out.println(n); Delay One second Thread.sleep(1000); To be caught when sleep() } is called } catch (InterruptedException e) { System.out.println("Main thread interrupted"); Output } } } 12
  • 13.
    Applications of Javathreads • Monitoring the state of resources • Databases , servers etc • Listening to events • GUI, Networking • Operations that take a while to complete • Printing • Any application that is parallel in nature 13
  • 14.
    Summary • In thisclass, you have learnt • Thread class • Constructors of Thread class • Methods of Thread class • Fields of Thread class • The applications of Java threads 14
  • 15.
    Quiz 1. start() methodof Thread, in turn calls A. execute() method B. run() method C. stop() method D. None 15
  • 16.
    Quiz Contd.. 2. Whichof the following methods return a reference to the currently executing thread A. this() B. super() C. currentThread() D. No such method 16
  • 17.
    Quiz Contd.. 3. Whichof the following method must be overridden to create a user thread A. start() method B. run() method C. stop() method D. None 17
  • 18.
    Quiz Contd.. 4. Whichof the following methods context switch to another thread A. run() B. sleep() C. yield() D. No such method 18
  • 19.
    Frequently Asked Questions 1. List the methods of Thread class 2. List the different constructors of Thread class 3. List the fields of Thread class 4. What are the applications of Java threads ? 19
  • 20.
    Assignment • Create ajava thread, called TimerThread • TimerThread should • Print time continuously, for every second, in the following format • hh:mm:ss, where • hh is hours • mm is minutes • ss is seconds • Write a Java application to test the TimerThread 20