Threads in Java - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Threads in Java

Description:

Threads and processes are two examples of concurrency, we will be ... since you're copying and pasting anyways... What can happen to a thread? Threads can be: ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 19
Provided by: Will320
Category:
Tags: anyways | java | threads

less

Transcript and Presenter's Notes

Title: Threads in Java


1
Threads in Java
  • By William Francis

2
Concurrency
  • Concurrency is defined as software that can do
    multiple things at once
  • Threads and processes are two examples of
    concurrency, we will be talking about threads

3
What are they?
  • Threads are useful for running multiple sections
    of code simultaneously, for example, sorting an
    array while printing time out to the screen
  • Threads become more important in advanced
    applications, where there is a possibility for
    multiple actions at once

4
Date Example
  • http//www.realapplets.com/tutorial/ThreadExample.
    html
  • Note, you need an html with this code
  • lthtmlgt
  • ltbodygt
  • ltapplet codeThreadExample height 300 width
    300gt lt/bodygtlt/htmlgt

5
How do they work?
  • Quite simply, the CPU allots a very brief slot of
    time to each thread, and it only appears to be
    simultaneous to us, the code within the thread is
    executed as if it was the only process running

6
Why are they useful?
  • Think of a standard text editor, in your text
    editor the software must be capable of listening
    to keyboard input, while updating to the screen
    at the same time!
  • If your program could only do one thing at a
    time, your program would be constantly
    interrupted while trying to update to the screen,
    and nothing would be accomplished

7
Text Editor contd
  • Without using threads
  • Things would be constantly stopped by events
  • The user must wait until the computer has updated
    to the screen character, by character

8
Why havent I seen these before?
  • Actually, you have!
  • Every single program has at least one thread, a
    main thread, even the most basic of programs
    has at least one thread
  • Thread.sleep(1000) is a command that pauses the
    current thread

9
How do I make them?
  • Threads are most useful when they are actually
    separate classes
  • Use
  • threadName.start()
  • This will begin the run() method in the
    threadName class

10
Basic Commands
  • Thread.start()
  • Public void run()
  • Thread.sleep(int)
  • Thread.getName
  • Public static void yield()
  • Public void destroy() especially important for
    threads running endlessly!

11
See ThreeThread example
  • This thread example can be accessed by going to
    http//www.science.uva.nl/ict/ossdocs/java/tutoria
    l/java/threads/simple.html
  • Download ThreeThreadsTest.java and
    SimpleThread.java

12
How do you do that?
  • IN A SEPARATE CLASS
  • public class example implements Runnable
  • public void run()
  • System.out.println(hello)
  • Output hello by typing
  • (new Thread(new example())).start()
  • In your other program

13
Explained
  • The previous example showed a thread as an object
  • The
  • (new Thread(new example())).start()
  • Starts the run method in that class IF Thread has
    been extended
  • The code inside the run method is what is
    executed when .start() is called

14
Thats too complicated
  • It doesnt have to be a separate class
  • Implement Runnable
  • public class thread_example Implements Runnable
  • Include a run method without parameters
  • public void run()
  • System.out.println(hello)

15
Lets Make Our Own!
  • Lets try a simple Thread example

16
Isnt there an easier way?
  • Yes, but
  • causes problems
  • Limits the programmer
  • since youre copying and pasting anyways

17
What can happen to a thread?
  • Threads can be
  • interrupted
  • paused
  • made high/low priority
  • blocked from certain methods
  • many other features to prevent crashes
  • Wait for other methods

18
Links
  • http//webcem01.cem.itesm.mx8005/web/java/tutoria
    l/essential/concurrency/index.html
  • http//www.cs.clemson.edu/cs428/resources/java/tu
    torial/JTThreadEx.html
  • http//www.science.uva.nl/ict/ossdocs/java/tutoria
    l/java/threads/simple.html
Write a Comment
User Comments (0)
About PowerShow.com