Thread Properties                    1
ObjectivesOn completion of this period, you would beable to learn•   Thread class•   Constructors of Thread class•   Metho...
Recap• In the previous class we learnt  • Deadlock problem  • Threads and deadlock  • To avoid deadlock in threads        ...
Thread Class• As we know already, Java provides two ways of  creating threads  • By extending Thread class  • By implement...
Constructors of Thread Class• Thread()  • Allocates a new Thread object• Thread (String name)  • Creates a new Thread obje...
Constructors of Thread Class                                           Contd . . .• Thread (Runnable target, String name) ...
Methods of Thread Class• We have already seen several methods of Thread  class• We will look at some more methods• current...
Methods of Thread Class                                             Contd . . .• start()   • Used to start a thread   • ru...
Methods of Thread Class                                               Contd . . .• getName()  • Returns the name of the cu...
Fields of Thread class• While discussing thread priorities, we looked  at the constants of Thread• These are the fields of...
Example Program// A simple program to demonstrate the currentThread()    method// It also uses the setName() method       ...
Example Program                                                   Contd . . . try {      for(int n = 5; n > 0; n--) {     ...
Applications of Java threads• Monitoring the state of resources   • Databases , servers etc• Listening to events   • GUI, ...
Summary• In this class, you have learnt   •   Thread class   •   Constructors of Thread class   •   Methods of Thread clas...
Quiz1. start() method of Thread, in turn calls  A.   execute() method  B.   run() method  C.   stop() method  D.   None   ...
Quiz Contd..2. Which of the following methods return a  reference to the currently executing thread  A.   this()  B.   sup...
Quiz Contd..3. Which of the following method must be  overridden to create a user thread  A.   start() method  B.   run() ...
Quiz Contd..4. Which of the following methods context  switch to another threadA. run()B. sleep()C. yield()D. No such meth...
Frequently Asked Questions1.   List the methods of Thread class2.   List the different constructors of Thread class3.   Li...
Assignment• Create a java thread, called TimerThread• TimerThread should  • Print time continuously, for every second, in ...
Upcoming SlideShare
Loading in …5
×

ThreadProperties

325 views

Published on

Thread constructors
Applications of Threads

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
325
On SlideShare
0
From Embeds
0
Number of Embeds
50
Actions
Shares
0
Downloads
10
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

ThreadProperties

  1. 1. Thread Properties 1
  2. 2. ObjectivesOn completion of this period, you would beable to learn• Thread class• Constructors of Thread class• Methods of Thread class• Fields of Thread class• Applications of Java threads 2
  3. 3. Recap• In the previous class we learnt • Deadlock problem • Threads and deadlock • To avoid deadlock in threads 3
  4. 4. 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
  5. 5. 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
  6. 6. 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
  7. 7. 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
  8. 8. 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
  9. 9. Methods of Thread Class Contd . . .• getName() • Returns the name of the current thread• activeCount() • Returns the number of active threads in the current threads thread group• getThreadGroup() • Returns the thread group to which this thread belongs• getState() • Returns the state of this thread 9
  10. 10. 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
  11. 11. 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
  12. 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. 13. 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
  14. 14. 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
  15. 15. Quiz1. start() method of Thread, in turn calls A. execute() method B. run() method C. stop() method D. None 15
  16. 16. 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
  17. 17. 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
  18. 18. Quiz Contd..4. Which of the following methods context switch to another threadA. run()B. sleep()C. yield()D. No such method 18
  19. 19. Frequently Asked Questions1. List the methods of Thread class2. List the different constructors of Thread class3. List the fields of Thread class4. What are the applications of Java threads ? 19
  20. 20. 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

×