Computer Science Unit 4
Computer Science Unit 4
A thread in Java is the direction or path that is taken while a program is being executed.
1.By creating a thread class: Define a class that extends Thread class and override its run() method
with the code required by the thread.
2. By converting a class to a thread: Define a class that implements Runnable interface. The
Runnable interface has only one method ,run(), that is to be defined in the method with the code to
be executed by the thread
ArithmeticException
IOException
wait() sends the calling thread into the sleep mode. This thread can now be activated only by
notify() and notifyall() methods
notify() resumes the first a thread that went into sleep mode
notifyall() resumes all the threads that are in sleep mode. The execution of these thread happens as
per priority.
13.Name the thread methods that determine when the thread ends
Long Answers
1.Discuss exception handling in Java using try – catch – finally statements with syntax an an
example
FileInputStream
FileOutputStream
Connection
ResultSet
Socket
URL
3. Explain how do you create a new thread class by implementing runnable interface with an
example
10. What is a thread? List and explain the different states of thread
A thread in Java is the direction or path that is taken while a program is being executed.
Newborn State
When we create a thread object, the thread is born and is said to be in newborn State. The thread is
not yet scheduled for running.
Runnable State
The Runnable state means that the thread is ready for execution and is waiting for the availability of
the processor. If all threads have equal priority then they are given time slots for execution in round
robin fashion that is first come first serve manner.
Running state
Running means that the processor has given its time to the thread for its execution. The thread runs
until it relinquishes control on its own or it is preempted by a higher priority thread.
Block state
A thread is said to be blocked when it is prevented from entering into the Runnable state and
subsequently the running state. This happens when the thread is suspended, sleeping or waiting in
order to satisfy certain requirements
Dead State
Every thread has a life cycle. Running thread ends it’s life when it has completed executing its run()
method. It is a natural death. However we can kill it by sending the stop message to it at any state
thus causing a premature death to it. A thread can be killed as soon as it is born or while it is running
or even when it is in a not Runnable condition.
When threads try to use data and methods outside of themselves they may compete for the same
resources and may lead to serious problems.
For example, one thread may try to read a record from a file while another is still writing to the same
file. Depending on the situation , we may get strange results
Java enables us to overcome this problem using a unique technique known as synchronization
The keyword synchronised helps us to solve such problems by keeping a watch on such locations
For example the method that will read information from a file and the method that will update the
same file may be declared as synchronised
Example
..................
When we declared a method synchronised Java creates a monitor and hands it over to the thread
that cause the method first time. As long as the trade holds the monitor no other thread can enter
the synchronised section of the code. A monitor is like a key and the thread that holds the key can
only open the lock.
13. Explain different methods of throwable class
226
Page 209
Each flow of control may be taught of as a separate tiny program known as a thread that runs in
parallel to others
Java uses a keyword try to preface a block of code that is likely to cause an error condition and
“throw” an exception
A catch block defined by the keyword catch “ catches” the exception “ thrown “ by the try block and
handles it appropriately
Example
................
try
...............
The try block can have one or more statements that could generate an exception
If any one statement generates an exception, the remaining statements in the block are skipped and
execution jumps to the catch block that is placed next to the try block
The catch ok too can have one or more statements that are necessary to process the exception
Every try statement should be followed by atleast one catch statement; otherwise compilation error
will occur
The catch statement is passed a single parameter, which is reference to the exception object
thrown.
If catch parameter matches with the type of exception object then the exception is caught and
statement in the catch block will be executed
Otherwise Exception is not caught and the default exception handler will cause the execution to
terminate .
Creating a thread
Threads are implemented in the form of objects that contain a method called run ().
The run () method makes up the entire body of a thread and is the only method in which the
thread’s behaviour can be implemented.
{..................
...................( Statements for implementing thread)
...................}
The run() method should be invoked by an object of the concerned thread. This can be achieved by
creating the thread and initiating it with the help of another thread method called start ()
1.By creating a thread class: Define a class that extends Thread class and override its run() method
with the code required by the thread.
2. By converting a class to a thread: Define a class that implements Runnable interface. The
Runnable interface has only one method ,run(), that is to be defined in the method with the code to
be executed by the thread
We can make our class runnable as thread by extending the class java.lang.Thread
2.Implement the run method that is responsible for executing the sequence of code that the thread
will execute
3.Create a thread object and call the start method to initiate the thread execution