Threads in Java

Threads in Java

1. What is the difference between System.out and System.err ?

A. Both are used to display messages on the monitor. System.out is used to display normal messages
System.out.println(“This is nayanimuralidhar”);
System.err.println(“This is an error”);

2. What is the advantage of stream concept..?

A. Streams are mainly useful to move data from one place to another place. This concept can be
used to receive data from an input device and send data to an output device.

3. What is the default buffer size used by any buffered class ?
Ans). 512 bytes.

4.. What is serialization ?

A. Serialization is the process of storing object contents into a file. The class whose objects are
stored in the file should implement ‘serializable’ interface of

5.What type of variables cannot be serialized ?

A. Static and transient variables cannot be serialized.Once the objects are stored into a file, they can be later retrieved and used as and when needed.This is
called de-serialization.

6. What is IP address ?

A. An IP address is a unique identification number allocated to every computer on a network or Internet. IP address contains some bytes which identify the network and the actual computer inside the network.

7. What is DNS ?
A. Domain Naming Service is a service on Internet that maps the IP address with corresponding website names.

8. What is a socket ?

A. A socket is a point of conneciton between a server and a client on a network.

9. What is port number ?

A. Port number ia a 2 byte number which is used to identify a socket uniquely.

10. Which thread always runs in a Java program by default ?

A. main thread. A thread represents execution of statements. The way the statements are executed
is of two types: 1). Single tasking 2). Multi tasking.

11. Why threads are called light-weight ?

A. Threads are light-weight because they utilize minimum resources of the system. This means they take less memory and less processor time.

12. What is the difference between single tasking and multitasking ?

A. Executing only one job at a time is called single tasking. Executing several jobs at a time is called multi tasking. In single tasking, the processor time is wasted, but in multi tasking, we can utilize the processor time in an optimum way.

13. How can you stop a thread in Java ?

A. First of all , we should create a boolean type variable which stores ‘ false’ . When the user wants to stop the thread. We should store ‘true’into the variable. The status of the variable is checked in the run ( ) method and if it is true, the thread executes ‘return’ statement and then stops.

14. What is the difference between ‘ extends Thread’ and ‘implements Runnable’ ? Which
one is advatageous ?

A. extends Thread and implements Runnable – both are functionally same. But when we write extends Thread, there is no scope to extend another class, as multiple inheritance is not supported in Java. Class Myclass extends Thread, AnotherClass //invalid If we write implements Runnable, then still there is scope to extend another class. class Myclass extends AnotherClass implements Runnable
This is definitely advantageous when the programmer wants to use threads and also wants to access the features of another class.

15. Which method is executed by the thread by default ?

A. public void run( ) method.

16. What is Thread synchronization ?

A. When a thread is already acting on an object, preventing any other thread from acting on the same object is called ‘Thread synchronization’ or ‘Thread safe’ The object on which the threads are synchronized is called ‘synchronized object’. Thread synchronization is recommended when multiple threads are used on the same object(in multithreading).

17. What is the difference between synchronized block and synchronized keyword ?

A. Synchronized block is useful to synchronized a block of statements. Synchronized keyword is useful to synchronize an entire method.

8). What is Thread deadlock ?

A. When a thread has locked an object and waiting for another object to be released by another thread.and the other thread is also waiting for the first thread to release the first object, both the threads will continue waiting forever. This is called ‘Thread deadlock’.

19). What is the difference between the sleep( ) and wait( ) methods ?

A. Both the sleep( ) and wait( ) methods are used to suspend a thread execution for a specified time. When sleep( ) is executed inside a synchronized block, the object is still under lock. When wait( ) method is executed, it breaks the synchronized block, so that the object lock is removed and it is available. Generally, sleep( ) is used for making a thread to wait for some time. But wait( ) is used in connection with notify ( ) or notifyAll( ) mehtods in therad communication.

20. What is the default priority of a thread ?

A. When a thread is created, by default its priority will be 5.

21). What is demon thread ?

A. A daemon thread is a thread is a thread that executes continuously. Daemon threads are service providers for other threads or objects. It generally provides a background procssing.

22). What is thread life cycle ?

A. A thread is created using new Thread( ) statement and is executed by start( ) method. The thread enters ‘runnable’ state and when sleep( ) or wait( ) methods are used or when the thread is blocked on I/O, it then goes into ‘not runnable’ state. From ‘not runnable’ state, the thread comes back to the ‘runnable’ state and continues running the statements. The thread dies when it comes out of run( ) mehtod . These state thransitions of a thread are called ‘life cycle of a thread’.


Post a Comment