Unit III - OOPs - Updated On 4.1.24 (Reg 21)
Unit III - OOPs - Updated On 4.1.24 (Reg 21)
Programming
Unit III – Exception Handling and Multi
Threading
By
D. Durai kumar,
Head of the Department
Department of Information Technology
Ganadipathy Tulsi’s Jain Engineering College
Vellore
Where,
ExceptionDemo : The class name
main : The method name
ExceptionDemo.java : The filename
java:5 : Line number
2. catch block:
Your code can catch the exception using catch and
handle it in some rational manner.
System-generated exceptions are automatically thrown
by the Java runtime system.
By D. Durai kumar, HOD / IT
3.2 Exception Hierarchy
Key words used in Exception handling
3. throw:
To manually throw an exception, use the keyword throw.
4. throws:
Any exception that is thrown out of a method must be
specified as such by a throws clause.
5. finally:
Any code that absolutely must be executed before a
method returns is put in a finally block
Thread:
A thread is a single sequential (separate) flow of control
within program.
Sometimes, it is called an execution context or light
weight process.
2.Runnable State:
– If a thread is in this state it means that the thread is
ready for execution and waiting for the availability of
the processor.
– If all threads in queue are of same priority then they
are given time slots for execution in round robin
fashion
General Syntax :
synchronized(object)
{
//statement to be synchronized
}
Thread Synchronization
Synchronized method
If you declare any method as synchronized, it is
known as synchronized method.
Synchronized method is used to lock an object for any
shared resource.
When a thread invokes a synchronized method, it
automatically acquires the lock for that object and
releases it when the thread completes its task.