100% found this document useful (1 vote)
112 views

Threads On Java

This document discusses thread programming in Java. It introduces threads as a way to perform multiple tasks simultaneously. There are two main ways to create threads: extending the Thread class or implementing the Runnable interface. The document outlines common applications of multithreading like animation, server requests, and browser tasks. It also describes the thread life cycle and how threads transition between states like running, waiting, and dead.

Uploaded by

tn_tylernguyen
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
112 views

Threads On Java

This document discusses thread programming in Java. It introduces threads as a way to perform multiple tasks simultaneously. There are two main ways to create threads: extending the Thread class or implementing the Runnable interface. The document outlines common applications of multithreading like animation, server requests, and browser tasks. It also describes the thread life cycle and how threads transition between states like running, waiting, and dead.

Uploaded by

tn_tylernguyen
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 10

Thread Programming in Java

Peter Liu
Seneca@York
Introduction
Showcases: teachingdemo.html, Bouncing2.java

the goal of thread programming (application level):

doing several things at once
Applications
an applet that does animation
- a thread is used to carry out the task of drawing pictures
the tasks of a multi-threaded server program
- multiple threads are used to process the requests of
multiple clients
Java servlet technology
- a servlet container creates multiple threads to process
multiple user requests for the service of a Java servlet



Applications
RMI technology
- JVM creates one or more threads to execute remote
requests
the concurrent tasks of a web browser program
- scroll a page
- download an applet
- play animation and sound
the concurrent tasks of a responsive interactive program
- monitor GUI events
- calculations as requested
- I/O

What is a Thread?
a thread:
- a path of execution
- an execution of a sequence of instructions
a multi-threaded program
-use of multiple threads in a single program
hardware level
-only one CPU is used to execute the threads!

How to create and start a thread?
option 1: extends Thread
- override the run( ) method
- example: ThreadDemo.java, ThreadDemo1.java
option 2: implements the Runnable interface
- implement the run( ) method
- pass the Runnable object into theThread constructor
- example: ThreadDemo3.java
Thread: an implementation of the Runnable interface
The Thread Class
some static methods
- sleep(long milliseconds)
- state: running => sleeping
- expiration of time:
- state: runnable <= sleeping
- getName( )
- currentThread( )
InterruptedException
thrown when a thread is waiting, sleeping, or
otherwise paused for a long time and another thread
interrupts it using the interrupt( ) method in class
Thread (API documentation)
a checked exception
The Life Cycle of a Thread
Java Tutorial: Fig. 82 (or Deitel & Deitel: Fig. 15.1)
states
- born( i.e. a new thread)
- runnable/ready
- running
- not runnable
- sleeping
- waiting
- blocked
- dead
- exit from the run( ) method
- uncaught exception

The Life Cycle of a Thread
state transitions triggered by the methods
- sleep( ): running => sleeping
- wait( ) : running => waiting
- notify( ), notifyAll( ) : waiting => runnable
- yield( ) : running => runnable
- interrupt( ): running => runnable
- I/O: running => blocked
- completion of run( ): running => dead

You might also like