Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Creating threads
Process and Threads
Thread life cycle
Thread priorities
Synchronizing threads
Multitasking
Multitasking:
refers to a computer's ability to perform multiple jobs concurrently
Multitasking is divided into two types:
What is Thread?
What is Multithreading ?
CPU1
CPU2
main
run
Process 1
Process 2
Process 3
GC
Process 4
Single and
Multithreaded
Processes
THREADS
4: Threads
11
Why Multithreading?
When multiple events/actions need to
occur at the same time
Examples:
Download 10 pages.
Single-threaded program: sequentially
Multithreaded: all at the same time
save time
Download data from the network and
respond to mouse at the same time
Beginning
Singlethreaded
Body of
Execution
End
A Multithreaded Program
Main Method
Module
start
start
switching
start
switching
Creating Threads
:: Extending the thread class
Threads
Execution of threads:
thr1.start();
Create
and Execute:
new MyThread().start();
Creating Threads
:: Extending the thread class : example
Creating Threads
Thread Object:
Thread thr1 = new Thread( myObject );
Start
Execution:
thr1.start();
17
EXAMPLE
ThreadEx2.java
Creating Threads
:: Implementing the runnable interface : example
} // end main()
// end class ThreadEx2
MultithreadedEXAM
PLE
ThreadTest.java