Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
68 views

Java Threads

A Java thread is a source of instructions that can run concurrently with other threads. The Thread class provides methods to create and control threads. A runnable object can also be used to run code concurrently by implementing the Runnable interface and passing the runnable to a Thread. Threads have lifecycle states like new, runnable, running, blocked, and dead. Methods like sleep(), join(), start(), stop() interact with and control threads. Threads can interact with GUI elements like Swing but require synchronization.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views

Java Threads

A Java thread is a source of instructions that can run concurrently with other threads. The Thread class provides methods to create and control threads. A runnable object can also be used to run code concurrently by implementing the Runnable interface and passing the runnable to a Thread. Threads have lifecycle states like new, runnable, running, blocked, and dead. Methods like sleep(), join(), start(), stop() interact with and control threads. Threads can interact with GUI elements like Swing but require synchronization.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

JAVA THREADS

Java thread

A Java thread is source of instructions


(sequential program) that can run
concurrently with others)
Java and threads
The Java programming language and the Java
environment (JVM) provide support for multithreaded
programming
Although the details
are implementation-
dependent, the JVM
is supposed to map
java-threads onto
OS-threads.
Class Thread
 The class java.lang.Thread provides the operations to
create, initialize and control the executions of threads.
 The program code executed by a thread is provided by
the method run(). Subclasses of Thread redefine this
method in order to specify the computation to be
performed.
FIRST EXAMPLE:
“main” launches a new
thread
Redefinition of run
method. Describes
computation to be
started and
performed in a
separate thread.
The main method is
executed in its own
(separate) thread too
Starting and stopping threads
 Once created, invoking the method start() causes the
thread to execute its run() method as an independent
activity, concurrent with the thread that called start()

 When the end of run() is reached, the thread


terminates. Terminated threads cannot be restarted.

 besides reaching the end of its run() method, other


events can terminate a thread: an invocation of its
stop() method or an uncaught exception.
Main runs in its own thread (it
may be regarded as having a
processor of its own)
When a new thread is
created a “new processor” is
allocated to its run method
When the thread is started Both threads can now
(by another thread) it proceed concurrently.
becomes ready to be
executed in its own processor.
SECOND EXAMPLE:
“main” launches two
threads that
interleave/overlap their
executions
The newly created threads will
execute the same code over
different objects. As nothing is
shared between the objects
(threads) no interference can arise
THIRD EXAMPLE:
The interface Runnable
The Runnable Interface
If a class cannot extend Thread because it is extending
any other class then it can implement the Runnable
interface.
 To be runnable a class has to implement a run() method.
 A thread can be created using a runnable object

public class RunCla extends XYZ implements Runnable {...}


...
RunCla runObj = new Xxx(...);
Thread th = new Thread (runObj)
th.start(); // concurrently exectute the run method of runObj
To be Runnable a class must
implement a run() method.
Threads can be created from
Runnable objects.

Start running the run method of the


runnable parameter in the
constructor.
FOURTH EXAMPLE:
Threads can interact with
the GUI environment
(SWING)
(but must be careful when doing so…)
This code is executed
when the GO button is
pressed

Being part of a listener this code is execute by


(inside) the AWT-managing thread (The Event
Dispatch Thread)
Threads and thread states

Like processes, threads are scheduled and are in one of several


states. Since they are created (new...) till they die (reach the end of
run()) they may go through different states.
Thread lifecycle (slightly simplified)
Thread lifecycle
runnable
get the lock blocked/waiting

start() resume()
new Thread(…)
not-running thread t terminates
(ready) sleep done
o.notify(), o.notifyAll()
interrupt()
(set bit) interrupt()
yield(), or
preempted scheduled
by JVM (throw exception)
by JVM
o.wait()
sleep(…)

running t.join()
stop(),
terminated suspend()
run() exits
normally or blocked by lock
abnormally
FIFTH EXAMPLE:
Threads can be put to
sleep, suspended and
resumed and stopped
...
...

Sleep is a static method. It puts the


invoking thread to sleep (a thread
can only put itself to sleep)
Suspend and resume are
instace-methods. A thread
may use them to
supend/resume other
threads.
Stop is an instance method. A
thread may use it to stop (kill)
other threads.
This invocation to setTimeToSleep
does not put b to sleep. Just suggests a
sleeping time... It is for b to decide when it
wants to go to sleep
stop, suspend and resume
are deprecated

But don’t worry...


Alternatives exist
SIXTH EXAMPLE:
An “animated” JPanel
The small ball moves!!!
... To be Runnable a class must
implement a run() method.

Repeatedly change position, force


repainting and sleep for a while (so
that the movement is not too fast)
...
ball is an instance of Ball (it’s one
of the BallLauncher’s private
components

...

You might also like