Title: MultiThreading
1Multi-Threading
- Threads, operating Systems and processors.
- Using Java to create and start new threads
- Controlling Threads
- Controlling Threads in groups
- Synchronising
- Threads in Animation.
2Threads, Operating Systems processors
- Computers appear to be able to do several things
at once. Mulitasking. - Each sequence of tasks performed by the CPU is
called a thread. - The priority and execution of threads is
controlled by the operating system. Windows and
Unix both support multithreading. - When the computer has more than one CPU then it
can truly perform several tasks at once.
Otherwise it is an illusion.
3MultiTasking
- One CPU can only perform one task at a
time.Nevertheless it can appear to be performing
many by sharing small portions of its time
between each. This is controlled by the operating
system which starts processes, monitors and
assigns priorities to them. - The CPU has a lot of idle time waiting for other
events. Multithreading uses this time to perform
other tasks without over taxing the CPU.A
restaurant chef can cook several meals at once,
eg. cutting up vegetables while waiting for the
rice to cook.
4Java and Multithreading
- Java allows us to start and control new threads.
Javas multithreading commands work through the
operating system. - All programs create a new thread when they are
started but this usually occurs from the
operating system. Java allows us to start new
Java threads from within a Java program. - When a Java program begins the main or init
methods are called from the JVM. - Java classes that are to be run as threads do not
have a main or init method, they have a run
method that is the first method to be executed.
5Java Threads
- Thread classes can be made in two ways
- By extending the Thread class.
- By implementing the Runnable interface.As Java
programs can only be extended from one class,
this frees the class to be extended from a
different class such as JFrame or JAplett.
6Threads by extending Thread
- The threaded class inherits a number of methods
from Thread.
Must catch InterruptedException
7Threads by extending Thread(2)
class TestThread p. s. v. main(String
args) //-- Create thread objects Thrd1 t1 new
Thrd1() Thrd2 t2 new Thrd2() //-- Start
Threads t1.start() t2.start()
class Thrd1 extends Thread public void run()
for (int n0nlt100n) System.out.println(
Port)
class Thrd2 extends Thread public void run()
for (int n0nlt100n) System.out.println(
Crows)
8Threads by implementing Runnable
- By extending Thread we use up the single
inheritance option available to our class. - By implementing an Interface we can use the
extends option for another class. - The Runnable interface has only one abstract
member, run(). - Access to the other methods, start(), wait()
notify() etc is obtained by wrapping an object of
the class in a Thread class.
9Threads by implementing Runnable (2)
class TestRunnable p. s. v. main(String
args) //-- Create thread objects Thread t1 new
Thread(new Thrd1()) Thread t2 new Thread(new
Thrd2()) //-- Start Threads t1.start()
t2.start()
Class wrapped in Thread Object
class Thrd1 implements Runnable public void
run() for (int n0nlt100n)
System.out.println(Port)
class Thrd2 implements Runnable public void
run() for (int n0nlt100n)
System.out.println(Crows)
10Thread State Diagram
Thread Created
Ready
finished
Start
Yield
run
Stop or finish
stop
New
Running
paused
Suspend Sleep or Wait
11Thread Groups
- Often there will be a number of threads combining
to serve a particular purpose. These can be
controlled as a group. - Create a ThreadGroup object and allocate a
name. - ThreadGroup tg new ThreadGroup(Thread
Group) - Add runnable objects to the group when you
create the Thread object via its
constructor.Thread t new Thread (tg, new
Thrd1())
12Thread Groups (2)
- A Thread group is itself a runnable object with
all the methods of normal threads. - You can start, wait, sleep, notify etc with the
whole group via the group object name. - As it is runnable it can be added to other Thread
groups so building up a tree or hierarchy of
groups that can be controlled at different levels.
13Synchronization
- Where many processes are using and updating the
same data errors can arise. - One program reads data, and takes some time
processing it before updating it. Meanwhile
another process has opened the same data before
the first process is complete. When the data is
updated it will soon be overwritten by the other
process. - Synchronization prevents a method from being
called on the same data before other processes
are finished.
14Synchronization (2)
- The method header of the method that updates the
data includes the keyword synchronized. This
prevents this method being run till the data is
released from another process. - public static synchronized void updateData()
-
- The static keyword ensures that the method is
locked in all instances of the class.
15The Timer class
- Belongs to the javax.swing package.
- Creates ActionListener events at a set period
which are caught by the actionPerformed method. - Register with the ActionListener in the
constructor.Timer t - t new Timer(1000,this)