The wonderful world of threads - PowerPoint PPT Presentation

1 / 7
About This Presentation
Title:

The wonderful world of threads

Description:

The wonderful world of threads. What is a thread, exactly? A piece of thin material used for holding together fabric? A message topic on a bulletin board? ... – PowerPoint PPT presentation

Number of Views:69
Avg rating:3.0/5.0
Slides: 8
Provided by: cseBu
Category:

less

Transcript and Presenter's Notes

Title: The wonderful world of threads


1
The wonderful world of threads
  • What is a thread, exactly? A piece of thin
    material used for holding together fabric? A
    message topic on a bulletin board? A discreet
    process fragment? All of these are true, but
    only one is interesting to us tonight.
  • You all brought your needles, right?

2
Ok, maybe not.
  • A java program consists of one or more threads.
    A thread is a single sequential flow of control
    within a program.
  • Threads are like processes inside of the
    application, but they dont get their own memory.
    Each does have its own stack.
  • Most applications have only one thread, the Main
    thread.

3
So why do we need more?
  • I tell ya, in my day, we didnt have no fancy
    threads. We just worked twice as hard in one!
  • You will need to use threads any time you want
    your program to perform two operations
    concurrently.
  • Besides, it sounds really cool to say that your
    program is multithreaded.

4
Can I get an example, please?
  • Timers
  • Polling jobs (listeners)
  • Background jobs

5
Using Thread
  • The simplest option is to use methods declared in
    the Thread class, or to use high level thread
    APIs like the java.util.Timer().
  • These APIs are very specific to certain tasks,
    however, and arent particularly customizable.
  • So, you probably want to make your own.

6
So, how do I make one?
  • In java, you just need to extend the class
    Thread.
  • The code you want your thread to execute goes in
    the run() method. You start and stop your thread
    with start() and stop(), respectively.
  • Your new thread will get spawned from another
    thread, probably the Main thread. This means
    that the Main thread will declare the new Thread
    object and call start() and maybe stop(), on it.

7
Code Example
  • Why dont we look at an implementation of a
    timer.
  • Why the heck couldnt I have gone over this
    before the project was due? Because Im mean.
Write a Comment
User Comments (0)
About PowerShow.com