0 ratings0% found this document useful (0 votes) 25 views37 pagesMulti Threading
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
Multithreaded
Programming and
Exception Handling
Creating Thread, Extending a thread class,
wutithreading
sing and Blocking a thread, Life cycle of thread, Using
method, Thread e:
ronization, Implementing a “Runnable” Interface,
ing Errors and Exceptions : Types of errors, Exception
ch. statement, using throw, throws and finally |
ssiement using finally statement, Using Exception for |
ions, Thread priority, |
Statistical Analysis
MSBTE Papers | Marks allotted for this
chapter
20 Marks
20 Marks
Winter 2009 24 Marks
} Summer 2010 24 Marks
Winter 2010 24 Marksin a thread based multitasking
thread is
‘een, contains only +
There are two types of
1s make
based multitaskin€executing several
programs at a time using
more than one processor.
1 |The operating system
allow hundreds of
among various
Multithreading is th®Mut Proge & E1cep. Hana,
Multithreading
executed, srpreter ha
the threads¢ Thread currentThread()
rns a reference to the thread in which
yu have the reference to the main
be controlled like other threads.
¢ the program s
id threads are executd
browsers such
executed by the thread.
‘Threads are implemented 18
contain a method called Tut
tain e‘dan
pees
/jTiwead code
Create a thread
method to
Following stateme
read
i. MyPirstThread T= ne" ence
un iostapen OF mnaioars inv
method.
des following steps
s extending the Thresd
‘an be extended
—
Thread is created..
s
v
Ken Si kx
printin (*\t From Thread C: kai)
them anymore bestse
yon their own. The cuit of
sequentially or do net
reruns
\ From Thread 8 : j=
m 8°)System.outpri
}
class RunnableTest
ble = new A();
ad = new Thread (uma)
Program threade ‘are 6
nable read rented
thd of main thread
ThreadA :
ThreadA =
ThreadA =
ThreadA =
ThreadA ¢
ThreadA +
ThreadA =
ThreadA ¢03==0)))
('Non-Primes" +);
Sistem. printin Primer +i);ken 5 ke)
1)
(1000);
}
catch( Exception ®)(F
cv
stem.out.printineee a ee
“catch ( InterruptedExcoption @)
catch (InterruptedExcop
; — pase ae
4.1.7 Thread Exceptions
© Mostly cal
TT jparesitme
wil: aaa
wer _attonot
method that a thread cannot baa
'ThreadStateException whene
kcrelinquish control -
examined, and the
lat is ready to run is
y a higher priority
lower priority thread
rocessor is simply
at is doing- by a lower
48 the higher priority
one of the following
1. Testops ronningtay
2 Teis made toslee?™
wait using
3. Ieis told
Whenever highe® Papel
running thread ¥en ri
‘And ths it freesProgram 5 : Using priority in threads,
ds Thread
| ‘hreadx.setPriority(Tt
System.out.printinive. Throw the
error information i.e, catch the
i.e. Handle the exception.
wn but exception objet
‘d. properly, the Java
appropriate
: Your code can
dle it in some rational manee
catel
catch and hat
throws : Any
‘method must be
clause.irown” by the try bl
lock
is added immediate!
Exception-typ
-typel
‘0Cess the exception
*)
atch
1 Bebtion-type
parameter, whicl
thrown by the try block.
matches with the type of
exception is caught and
will be executed. Othe
and the default exception ™
execution to terminate, vit in ct
All exception types are subeaes of re
‘Throwable. Thus, the Thon erie OD
exception class hierarh¥-
the Throwable ~ ©
andles wihmaticException e )
Print vision by 20")rows IlegatAccessception{
side Throw One);
ccessException ‘Deno’
throwswithn a try block
procB() {
yt
Systemout printn( "Inside proc®")
Vinal {
| system out. printin(“procB Fal)int x = 5, y= 1000 ;
| ty
i {
| float z = ( float ) */
I if (z<0.01)
{
throw new MyException ( “ No. is too smal
} :
426
( MyException e )
in ( "Caught my exception” ) +
e.getMessage ()
("Tam always here’
system.out.printin© throw :
=e Wit program 19
fit caters | ening ron —
_ descending number Be
2°
fing extends invead
Public void run()‘order and other in
0 fo pit mumbo |
trom 1 fo 50.new printwithdelay
Explain ite
Distinguish b=
0 we
10 write @ Pome fut
using wv