Professional Documents
Culture Documents
2: Java Thread CS508
2: Java Thread CS508
CS508
Robert Zhu
Setting up your Workspace
2.2
Some important concepts about Eclipse
Environment
Workbench
Perspective
Views
Editor
…
2.3
Workbench
2.4
Understanding Perspective
Each Workbench window contains one or more perspectives
For example, the Java perspective combines views that you would
commonly use while editing Java source files, while the C/C++
perspective contains the views that you would use for editing C++
files
2.5
Built-in Eclipse Perspectives
Resource
Arrange your files and projects.
Java
Develop programs in the Java language.
Debug
Diagnose and debug problems that occur at runtime.
Java Browsing
Java Type Hierarchy
Plug-in Development
CVS Repository Exploring
Team Synchronizing
2.6
Views
2.7
Editors
2.8
Creating Java Projects
2.9
Creating Java Packages
2.10
Adding Java Classes
2.11
Import existing files
Some files are given and you want to import them
to your projects rather than creating them again
2.12
Running the code
2.13
Other issues
Demo:
Removing files from the project
Check (or not) “Build Automatically”
Window->Preferences
Project->Properties
…
Eclipse provides a very good Help system
“Help->Help Contents” for more information
2.14
Threads
2.15
Problem
2.16
Multitasking (Time-Sharing)
Approach
Computer does some work on a task
Computer then quickly switch to next task
Tasks managed by operating system
(scheduler)
Computer seems to work on tasks
concurrently
Can improve performance by reducing
waiting
2.17
Multitasking Can Aid Performance
Single task
Two tasks
2.18
Multiprocessing (Multithreading)
Approach
Multiple processing units (multiprocessor)
Computer works on several tasks in parallel
Performance can be improved
2.19
Perform Multiple Tasks Using…
1. Process
Definition – executable program loaded in
memory
Has own address space
Variables & data structures (in memory)
Each process may execute a different program
Communicate via operating system, files,
network
May contain multiple threads
2.20
Perform Multiple Tasks Using…
2. Thread
Definition – sequentially executed stream of
instructions
Shares address space with other threads
Has own execution context
Program counter, call stack (local variables)
Communicate via shared access to data
Multiple threads in process execute same
program
Also known as “lightweight process”
2.21
Motivation for Multithreading
2.24
Programming with Threads
Concurrent programming
Writing programs divided into independent tasks
Tasks may be executed in parallel on multiprocessors
Multithreading
Executing program with multiple threads in parallel
Special form of multiprocessing
2.25
Creating Threads in Java
Two approaches
Thread class
public class Thread extends Object { … }
Runnable interface
public interface Runnable {
public void run(); // work thread
}
2.26
Thread Class
2.27
More Thread Class Methods
2.28
Creating Threads in Java
1. Thread class
Extend Thread class and override the run method
Example
public class MyT extends Thread {
public void run() {
… // work for thread
}
}
MyT T = new MyT () ; // create thread
T.start(); // begin running thread
… // thread executing in parallel
2.29
Creating Threads in Java
2. Runnable interface
Create object implementing Runnable interface
Pass it to Thread object via Thread constructor
Example
public class MyT implements Runnable {
public void run() {
… // work for thread
}
}
Thread T = new Thread(new MyT); // create thread
T.start(); // begin running thread
… // thread executing in parallel
2.30
Creating Threads in Java
Note
Thread starts executing only if start() is called
Runnable is interface
So it can be multiply inherited
Required for multithreading in applets
2.31
Threads – Thread States
Java thread can be in one of these states
New – thread allocated & waiting for start()
Runnable – thread can begin execution
Running – thread currently executing
Blocked – thread waiting for event (I/O, etc.)
Dead – thread finished
Transitions between states caused by
Invoking methods in class Thread
new(), start(), yield(), sleep(), wait(), notify()…
Other (external) events
Scheduler, I/O, returning from run()…
2.32
Threads – Thread States
State diagram
new start
notify, notifyAll,
new runnable
IO complete,
sleep expired,
yield,
scheduler join complete
time
slice
running blocked
IO, sleep,
terminate wait, join
dead
2.33
Daemon Threads
2.34
Threads – Scheduling
Scheduler
Determines which runnable threads to run
Can be based on thread priority
Part of OS or Java Virtual Machine (JVM)
Scheduling policy
Nonpreemptive (cooperative) scheduling
Preemptive scheduling
2.35
Threads – Non-preemptive Scheduling
2.36
Threads – Preemptive Scheduling
2.37