CIS219 - PowerPoint PPT Presentation

About This Presentation
Title:

CIS219

Description:

CIS219 Last lecture-16 December 2005 Threads What are they? How do we make them in Java? Interface Runnable Class Thread Controlling threads? Start Stop Sleep What ... – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 13
Provided by: docGoldA1
Category:
Tags: cis219 | threads

less

Transcript and Presenter's Notes

Title: CIS219


1
CIS219
  • Last lecture
  • -
  • 16 December 2005

2
Threads
  • What are they?
  • How do we make them in Java?
  • Interface Runnable
  • Class Thread
  • Controlling threads?
  • Start
  • Stop
  • Sleep

3
What are threads in Java
  • If you need several things happening at once
  • Example Listeners
  • Threads are processes
  • Think about Windows
  • You can play music and write Java at the same
    time

4
How to make a Object a Thread?
  • There are two basic ways
  • With the Interface Runnable
  • Class Thread
  • Since Class Thread needs to be extended and
    inheritance via extension is a scarce resource we
    prefer the use of Interface Runnable
  • However to extend a class makes some thing
    easier.
  • Example Sleep function

5
Class Thread
  • Check API !!
  • Class Rect extends Thread
  • // This is all you need to give a class the
    ability to become a thread
  • If we read the API we see
  • public class Thread extends Object implements
    Runnable
  • So what happens when we make a thread?

6
Thats what happens
  • class ThreadRect extends Thread
  • public void run()
  • ....            
  • Everything we want to be done in the thread we
    put into the run() method
  • It will get called when the Thread gets started
  • We will learn later how to start a thread

7
So lets see how to start a Thread
  • ThreadRect p new ThreadRect()
  • p.start()
  • Now we started the Thread
  • Everything in the run method will get executed

8
Runnable interface
  • Generally this method of making threads is used
    since we can only extend from one class but
    implement many
  • Trade of
  • We cannot use all the methods from Class thread
    as easily later more
  • Let see how to make Threads in this way

9
Using Runnable
  • Again check the API !!
  • There we see
  • public interface Runnable
  • It has only one method
  •  void run()
  • // since it is not specified, its public
  • // since all methods in an interface are abstract
    and not
  • // implemented, we need to do that and implement
    them
  • So our class would look like this

10
Using Runnable
  • class ThreadRect implements Runnable
  • public void run()
  • ....            
  • So how can we start this Thread?

11
One last thing
  • Boolean keepAlive true
  • ...
  • public void run()
  • while(keepAlive)
  • // keep on running
  • ...
  • Note
  • We need to hava condition on what we can
    terminate the thread and that keeps the thread
    alive or running. So a boolean is a neat
    solution for this problem.

12
Sarting A Thread II
  • ThreadRect p new ThreadRect()
  • new Thread(p).start()
  • Note that essentially we also start a thread here
    but that we loose the reference to the object
    as soon as it has been created
  • This makes it hard to apply methods such as sleep
    from the Thread class a a later point
Write a Comment
User Comments (0)
About PowerShow.com