0% found this document useful (0 votes)
50 views7 pages

Computer Science Unit 4

A thread in Java represents the flow of execution through a program. There are two main ways to create threads: 1) by extending the Thread class and overriding the run() method, 2) by implementing the Runnable interface. The wait(), notify(), and notifyAll() methods are used to pause and resume threads that are waiting on a monitor. A catch block defines how exceptions thrown in a try block are handled.

Uploaded by

yogithashettigar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views7 pages

Computer Science Unit 4

A thread in Java represents the flow of execution through a program. There are two main ways to create threads: 1) by extending the Thread class and overriding the run() method, 2) by implementing the Runnable interface. The wait(), notify(), and notifyAll() methods are used to pause and resume threads that are waiting on a monitor. A catch block defines how exceptions thrown in a try block are handled.

Uploaded by

yogithashettigar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Two mark questions

1.What is thread? Mention two ways to create new thread

A thread in Java is the direction or path that is taken while a program is being executed.

Two ways of creating threads

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

2.List and write the purpose of any two built in exceptions

ArithmeticException

Caused by the math errors such as division by zero

IOException

Caused by general I/O failures, such as inability to read from a file

3.What is the purpose of wait() , notifyAll() , notify() and isAlive() method

wait() sends the calling thread into the sleep mode. This thread can now be activated only by
notify() and notifyall() methods

final void wait()

notify() resumes the first a thread that went into sleep mode

final void notify()

notifyall() resumes all the threads that are in sleep mode. The execution of these thread happens as
per priority.

final void notifyall()

4.How do we define catch block? Give example

8.What is an exception ? Name the class at the top of exception heirachy

9.What is the purpose of transient modifier

10.List any 4 system defined exceptions in Java


11.What is synchronization with reference to multithreaded programming

12.Mention the class and interface that support multithreading

13.Name the thread methods that determine when the thread ends

14.What are the two ways of creating threads in java

15.What is exception .Name the two built in exceptions in Java

16.How will you change a threads priority

17.List 4 keywords used in exception handling

18.What does isAlive() method return

19.List any 4 types of exceptions in Java

Long Answers

1.Discuss exception handling in Java using try – catch – finally statements with syntax an an
example

2. Explain the following

FileInputStream

FileOutputStream

Connection

ResultSet

Socket

URL

3. Explain how do you create a new thread class by implementing runnable interface with an
example

4. Discuss the steps involved in developing a RMI application

5. Explain exception handling in Java with suitable example

Java uses a keyword try to preface a

6. List and explain any five thread methods

7. write a note on thread priority.

Explain the different thread priorities in Java


Page 209

8. List and explain any 5 built in exceptions in Java

9. Describe the use of finally with suitable 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.

11. Explain the purpose of synchronisation

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

synchronised void update ()

..................

.................// Code here is synchronised

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

14. illustrate the use of multiple catch statements with example

226

15. Explain how to set thread priorities with suitable example

Page 209

16. Explain how to create multiple threads with example

Explain multithreading in java

A unique property of Java is its support for multithreading

Java enables us to use multiple flows of each control in developing programs

Each flow of control may be taught of as a separate tiny program known as a thread that runs in
parallel to others

A program that contains multiple flows of control is known as a multith

17.Explain the process of exception handling in Java with example

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

The catch block is added immediately after the try block

Example
................

try

statement ; // generates and exception

catch (Exception type e)

statement ; // processes the exception

...............

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 .

18 Explain the process of creating a thread by extending the thread class

Creating a thread

Creating threads in java is simple.

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.

It’s general form

public void run()

{..................
...................( 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 ()

A thread can be created in two ways

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

Extending the thread class

We can make our class runnable as thread by extending the class java.lang.Thread

This gives us access to all the thread methods directly

It includes the following step

1.Declare the class as extending the thread class

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

You might also like