23 Question Related To Multithreading

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Question related to Multithreading

1. What is Thread from java point of view?

Ans:
Thread is a light weight sub process which is suitable for game & network
application. It is a separate path of execution.

2. Difference between Process and Thread in java?

Ans:
The difference between Process and thread is the running program of computer is
known as Process where Thread is a light weight sub process, a smallest unit of
processing. There can be multiple process inside the OS and one process can
have multiple threads.

3. What is the name of default Thread in java?

Ans:
main is the default thread in java.

4. Difference between void sleep () and void suspend () method in java?

Ans:
Sleep() method block the thread for a specific period of time Where as suspend()
method block the thread for a unknown period of time.

5. Difference between wait () and sleep () in java?

Ans:
The difference between wait() and sleep() method is wait() method cause thread
to release the lock and wait until either another thread invoked the notify() or
notifyAll() method for this object Where as sleep() method block the thread for a
specific period of time.

6. What are the ways in which a thread can enter the waiting state?

Ans:
Thread can enter the waiting state by 3 different ways:
i. A Thread can enter the waiting state by invoking its sleep () method.
ii. By blocking on I/O by unsuccessfully attempting to acquire an object's
lock or by invoking an object's wait () method.
iii. It can also enter the waiting state by invoking it's
suspend () method.

7. How can a dead thread be restarted?

Ans:
A dead thread cannot be restarted.

8. Explain isAlive() method under Thread class?


Ans:
Boolean isAlive() method check the thread is alive or dead.

9. Which method must be implemented by all threads?

Ans:
Run() must be implemented by all threads.

10. Why to use multithreading than multiprocessing?

Ans:
Threads share a common memory area so they save memory .switching between
the threads takes less time than process. So multithreading is more used then
multiprocessing.

11. How thread create in java?

Ans:
Thread able to create in java by 2 ways
i) By extending thread class.
ii) By implementing Runnable interface.

12. Define multithreading?

Ans:
When a process will divide into multiple path and each path able to execute
independently then it is known as Multithreading. IN other words Multithreading
is a process of executing multiple threads simultaneously.

13. Define Multitasking?

Ans:
Multitasking is a process of executing multiple tasks simultaneously.

14. How multitasking can be achived?

Ans:
Multitasking can be achieved by 2 ways
i) Process based Multitasking (Multiprocessing).
ii) Thread based Multitasking (Multithreading).

15. What are the diff. b/w multithreading and multiprocessing?

Ans:
i) Multithreading is the one in which there exist multiple flow of controls
whereas it is the one in which there exist single flow of control.
ii) Threads share the same address space where as Each thread have its own
address in memory.
iii) cost of communication between the threads are low where as s cost of
communication between the process is high.
iv) Thread is light weight. process is heavy weight

16. State the work of Newborn State, Active state, Block state and Dead state? OR
Describe the life cycle of Thread?

Ans:
New Born State - In this state thread may enter into Dead or Active state.
Active State - In this state thread actually execute.
Block State - In this state thread wait for some resource.
Dead State - In this state thread de allocates the memory.

17. Which method return object of thread class?

Ans:
Static thread currentThread() method retuens the object of Thread class.

18. State the work of void run() method?

Ans:
It is used to perform action for a thread.

19. What is the diff. b/w StringgetName() and void setName() method?

Ans:
String getName() method returns the name of Thread where as Void setName()
method assign a name to thread.

20. What is the diff. b/w intgetPriority() and intsetPriority() method?

Ans:
getPriority () method return the priority of the thread where as setPriority()
method change the priority of thread.

21. What is the diff.b/w Boolean isInterrupted() and Static isInterrupted() method?

Ans:
Boolean isInterrupted() method test if the thread bas been intertupted where as
static Boolean isInterruptedmethod() test if the current thread has been
interrupted.

22. State MAX_PRIORITY,MIN_PRIORITY,NORM_PRIORITY of thread?

Ans:
The maximum of priority of thread is MAX_PRIORITY which value is 10.
The maximum of priority of thread is NORM_PRIORITY which value is 5.
The maximum of priority of thread is NORM_PRIORITY which value is 1.
23. What is the difference between extending thread class and implementing
Runnable interface?

Ans:
One difference between implementing Runnable and extending Thread is that by
extending Thread, each of your threads has a unique object associated with it,
whereas implementing Runnable, many threads can share the same object
instance.

24. Which is better for thread extending thread class or implementing runnable
interface OR Which way is better to create a Thread and Why?

Ans:
When there is a need to extend a superclass, implementing the Runnable
interface is more appropriate than using extending Thread class. Because we can
extend another class while implementing Runnable interface to make a thread.
But if we just extend the Thread class we can't inherit from any other class.

25. How many thread execute at a single time?

Ans:
Only one thread can execute at a single time.

26. Who control lifecycle of thread?

Ans:
JVM control life cycle of Thread.

27. How many method present in Runnable interface & state the use of the methods?

Ans:
Runnable interface have only one method i.erun() method.it is used to perform
action for a thread.

28. What is the work of Start() method of thread class?

Ans:
Start() method make the thread eligible to run.

29. How to perform multiple task by multithreads?

Ans:
If you want to perform multiple tasks by multi threads then have to use multiple
run method.

30. How to perform single task by multithreads?

Ans:
If you want to perform single task by multi threads then have to use only one
run() method.

31. State the situation where multiclass object is Thread object?

Ans:
Thread class constructor allocate new thread object ,when multiclass object
create ,class constructor is invoked from where Thread class constructor is
invoked. This is the situation where multiclass object is Thread class.

32. What happened if you are not extending the thread class?

Ans:
If you are not extending the thread class then your class object would not be
treated as aobject.Then you need to create thread class object.

33. What is thread scheduler?

Ans:
Thread scheduler is the part of the JVM that decide which thread should run.

34. Which scheduling used by thread scheduler to schedule the thread?

Ans:
Thread scheduler mainly use preemptive or time slicing scheduling to schedule
the thread.

35. What is the diff. b/w Preemptive scheduling and time slicing?

Ans:
Under preemptive scheduling the highest priority task execute until it enter the
waiting or dead state or highest priority task comes into existence.
Under time slicing a task execute for a predefined slice of time and then re-enter
the pool of ready tasks.

36. Which two methods provides by the thread class for sleeping a thread?

Ans:
The two methods are
i) public static void sleep (long milisecond)throws InteruptedException.
ii) public static void sleep(long millisecond ,int names) throws
InteruptedException.

37. Can we start a thread twice and mention why?

Ans:
No we cannot start a thread twice because once a thread start it never be started
again because if we do so then an IlligalThreadStateException is thrown.
38. What If we call run() method directly instead of start() method?

Ans:
Each thread starts in a separate call stack. Invoking the run() method from main
thread, the run() method goes into the current call stack rather than at the
beginning of a new stack.

39. What is the work of join() method?

Ans:
The join() method wait for a thread to die.in other word it causes the currently
running thread to stop executing until the thread join with complete its task.

40. State the work of CurrentThread() method?

Ans:
The currentThread() method returns a reference to the currently executing
thread object.

41. What is Preemptive Scheduling?

Ans:
Thread scheduler schedules the threads according to their priority which is known
as preemptive scheduling.

42. Which is a low priority Thread?

Ans:
Daemon Thread is a low priority thread.

43. What is Daemon thread?

Ans:
Daemon thread in java are those thread which runs in background and mostly
created by JVM for performing background task like garbage collection and other
housekeeping task.

44. What is the difference between daemon thread and user thread?

Ans:
Main difference between both is as soon as all user thread finish execution java
program or JVM terminates itself, JVM doesn’t wait for daemon thread to finish
their execution.As soon as last non daemon thread finished JVM terminates no
matter how many daemon thread exists or running inside JVM.

45. What are the methods present in java for Daemon Thread?

Ans:
java. lang .Thread class provides two methods for Daemon thread. public void
setDaemon(Boolean status). public Boolean isDaemon ().

46. What is the diff.b/w void setDaemon() and Boolean isDaemon() methods?

Ans:
setDaemon (Boolean status) used to mark the current thread as Daemon thread
or user thread where Boolean isDaemon() used to check that current is daemon.

47. What to do if you want to make a user thread to Daemon?

Ans:
If you want to make a user thread as Daemon ,it must not be started otherwise
throw IlligalThreadStateException Daemon.

48. What is the use of TheShutdown Hook?

Ans:
The Shutdown Hook can be used to perform clean up resource or save the state
when JVM shut down normally or abruptly.

49. When JVM Shutdown?

Ans:
The JVM shut down in following situations
i) user logoff
ii) user shutdown
iii) System.exit (int)method invoked
iv) user press ctrl+c on the command prompt.

50. State the use of addShutdownHook() method?

Ans:
The addShutdown Hook () method of Runnable class is used to register the
thread with the virtual machine.

51. Define Synchronization & its type?

Ans:
Synchronization is the capability of control the access of multiple threads to any
shared resource. It is of 2 types:
i) process synchronization.
ii) Thread synchronization.

52. State use of Synchronization?

Ans:
Synchronization mainly used in case of
i) To prevent thread interference.
ii) To prevent consistency problem.

53. Define types of thread Synchronization?

Ans:
Thread synchronization are of two types
i) Mutual exclusive
ii) Inter thread communication.

54. How The Shutdown sequence can stop?

Ans:
The shutdown sequence can be stopped by invoking the halt(int) method of
Runtime class.

55. Define concept of Lock?

Ans:
Synchronization is built around an internal entity known as Lock.

56. What is the problem without Synchronization?

Ans:
If there is no Synchronization ,then output is inconsistent.

57. State Synchronized methods & its use?

Ans:
If you declare any method as synchronized then it is known as synchronized
method .it is used to lock an object for any shared resource.

58. State the use of Synchronized Block?

Ans:
Synchronized block used to perform synchronization on any specific resource of
the method.

59. When Synchronized block work same as Synchronized method?

Ans:
If we put all the codes of the method in the synchronized block then it will work
same as the synchronized method.

60. What is Deadlock?

Ans:
When thread are waiting for each other to release the lock,the condition is called
Deadlock.

61. What is Cooperation or Inter-thread communication?

Ans:
Cooperation is all about making synchronized threads communicate with each
other.

62. By which method Cooperation can implemented?

Ans:
Cooperation can be implemented by
i) wait()
ii) notify()
iii) notifyAll().

63. What is the diff. b/w notify() and notifyAll() methods?

Ans:
notify() use to wake up a single thread that is waiting for this objects monitor.
Where notifyAll() use to wake up all threads that are waiting on this objects
monitor.

64. What Wait() method do?

Ans:
wait() method cause thread to release the lock and wait until either another
thread invoked the notify() or notifyAll() method for this object.

65. What is the concept means “Interrepting a Thread”?

Ans:

66. What are the methods provide by thread class forinturrepting a Thread?

Ans:
Three method provided by the Thread class for inturrepting a thread

i) void interrupt()
ii) static Boolean interrupted()
iii) BooleanisInterrupted.

67. What are the state for Thread present in java?

Ans:
java provides 5 states. They are
i) New
ii) Ready
iii) Running
iv) Waiting
v) Halted or Dead state.

68. What is Halted state?

Ans:
In this state the thread completed its total execution.

69. State the ways to create the osbject of Thread class?

Ans:
The object of thread class can be created by 3 ways:-
i) Directly
ii) using factory method.
iii) using sub class of thread class.

70. How a programmer construct the object of Runtime class?

Ans:
If programmer want to construct the object of runtime class,the programmer
have to call a static method of the Runtime class.

71. Define System class?

Ans:
System class is a predefined class present in default package which holds some
predefined static method and variables.

72. State the methods present in System class &Define each?

Ans:
i) Public static void exit(int)-It halts the process.
ii) Public static void gc()-To call the garbage collector explicitly.
iii) Public static void loadLibrary()-Native method use.
iv) Public static java.lang.StringgetPriority()-This method returns the property
allocated with the string argument.

73. State the work of static java.lang.StringgetProperty() method?

Ans:
This method returns the property allocated with the string argument.

74. State the work of static long currentTimeMillis() method?


Ans:
It returns the current time in millisecond.

75. Define Process class?

Ans:
It is a predefined class present in default package. Developer occasionally needs
to write the application in some editor where as they want to run it some other
editor as it cannot be instantiated.

You might also like