Threads
1.What is difference between process-based multitasking and thread-based multitasking?
Process-based multitasking is used to run two or more programs concurrently. Thread-based multitasking, called multithreading, is used to run pieces of one program concurrently.
2.In general, in what states can a thread exist?
The thread states are running, ready-to-run,suspended,blocked, and terminated. When a suspended thread is restarted, it is said to be resumed.
3.What class encapsulated a thread?
Thread
4.In what two ways can you create a class that can act as a thread?
To create a thread, either implement Runnable or extend Thread
5.What is the purpose of the run() method defined by Runnable?
the run() method is the entry point to a thread
6. What does the start() method defined by Thread do?
The start() method starts the execution of thread
7.Are there different types of threads?
Java defines two basic types of threads: user and daemon.A user thread will continue to execute until it concludes. A daemon thread will be automatically terminated when all user threads have terminated. By default, a new thread is of the same type as its creating thread.You can change a thread to a daemon thread by calling setDaemon(), shown here:
final void setDaemon(boolean isDaemon)
if isDaemon is true, the thread becomes a daemon thread. Otherwise, it becomes a user thread. This method must be called before the thread's start() method is calle.
8.Why does Java have two ways to create child threads ( by extending thread or implementing Runnable) and which approach is better?
the Thread class defines several methods that can be overridden by a derived class. Of these methods, the only one that must be overriddeen is run(). This is, of course, the same method required when you implement
Runnable. Some Java programmers feel that classes should be extended only when they are being enhanced or modified in some way. So, if you will not be overriding any of Thread's other methods, it is probably best to simply implement Runnable. Also, by implementing Runnable, you enable your thread to inherit a class other than Thread.
9.What are the two ways in which you can determine whether a thread has ended?
To determine whether a thread has ended, you can call isAlive() or use join() to wait for the thread to join the calling thread.
10.Explain join()?
the join() method suspends execution of the calling thread until the thread on which join() is called ends.
11. Does the operating system's implementation of multitasking affect how much CPU time a thread receives?
One of the most important factors affecting thread execution is the way the operating system implements multitasking and scheduling. For example, it is common for an operating system to use preemptive multasking
in which each thread receives a time slice, at least occasionally. However, it is also possible for an operating system to use non preemptive scheduling in which one thread must yield execution before another thread will execute. If your multithreaded program is running in a non-preemptive environment, it is easy for one thread to dominate, preventing others from running.
12.How do you set a thread's priority?
To set a thread's priority, call setPriority()
13.How do you restrict access to an object to one thread at a time?
To restrict access to an object to one thread at a time, use the synchronized keyword.
14. The synchronized keyword can be used to modify a method or to create a __________ block.
synchronized
15.What methods support interthread communication?
The interthread communication methods are wait(), notify() and notifyAll().
16. Do all objects support interthread communication?
Yes, all objects support interthread communication because this support is part of Object.
17. What happends when wait() is called?
when wait() is called, the calling thread relinquishes control fo the object and suspends until it receives a notification.
18. Multithreading seems like a great way to improve the efficiency of my programs. Can you give me any tips on effectively using it?
The key to effectively utilizing multithreading is to think concurrently rather than serially. For example, when you have two subsystems within a program that are fully independent of each other, consider making them into individual threads. A word of caution is in order, however. If you create too many threads, you can actually degrade the performance of your program rather than enhance it. Remember, overhead is associated with context switching; If you create too many threads, more CPU tie will be spent changing contexts than in executing your program!
1.What is difference between process-based multitasking and thread-based multitasking?
Process-based multitasking is used to run two or more programs concurrently. Thread-based multitasking, called multithreading, is used to run pieces of one program concurrently.
2.In general, in what states can a thread exist?
The thread states are running, ready-to-run,suspended,blocked, and terminated. When a suspended thread is restarted, it is said to be resumed.
3.What class encapsulated a thread?
Thread
4.In what two ways can you create a class that can act as a thread?
To create a thread, either implement Runnable or extend Thread
5.What is the purpose of the run() method defined by Runnable?
the run() method is the entry point to a thread
6. What does the start() method defined by Thread do?
The start() method starts the execution of thread
7.Are there different types of threads?
Java defines two basic types of threads: user and daemon.A user thread will continue to execute until it concludes. A daemon thread will be automatically terminated when all user threads have terminated. By default, a new thread is of the same type as its creating thread.You can change a thread to a daemon thread by calling setDaemon(), shown here:
final void setDaemon(boolean isDaemon)
if isDaemon is true, the thread becomes a daemon thread. Otherwise, it becomes a user thread. This method must be called before the thread's start() method is calle.
8.Why does Java have two ways to create child threads ( by extending thread or implementing Runnable) and which approach is better?
the Thread class defines several methods that can be overridden by a derived class. Of these methods, the only one that must be overriddeen is run(). This is, of course, the same method required when you implement
Runnable. Some Java programmers feel that classes should be extended only when they are being enhanced or modified in some way. So, if you will not be overriding any of Thread's other methods, it is probably best to simply implement Runnable. Also, by implementing Runnable, you enable your thread to inherit a class other than Thread.
9.What are the two ways in which you can determine whether a thread has ended?
To determine whether a thread has ended, you can call isAlive() or use join() to wait for the thread to join the calling thread.
10.Explain join()?
the join() method suspends execution of the calling thread until the thread on which join() is called ends.
11. Does the operating system's implementation of multitasking affect how much CPU time a thread receives?
One of the most important factors affecting thread execution is the way the operating system implements multitasking and scheduling. For example, it is common for an operating system to use preemptive multasking
in which each thread receives a time slice, at least occasionally. However, it is also possible for an operating system to use non preemptive scheduling in which one thread must yield execution before another thread will execute. If your multithreaded program is running in a non-preemptive environment, it is easy for one thread to dominate, preventing others from running.
12.How do you set a thread's priority?
To set a thread's priority, call setPriority()
13.How do you restrict access to an object to one thread at a time?
To restrict access to an object to one thread at a time, use the synchronized keyword.
14. The synchronized keyword can be used to modify a method or to create a __________ block.
synchronized
15.What methods support interthread communication?
The interthread communication methods are wait(), notify() and notifyAll().
16. Do all objects support interthread communication?
Yes, all objects support interthread communication because this support is part of Object.
17. What happends when wait() is called?
when wait() is called, the calling thread relinquishes control fo the object and suspends until it receives a notification.
18. Multithreading seems like a great way to improve the efficiency of my programs. Can you give me any tips on effectively using it?
The key to effectively utilizing multithreading is to think concurrently rather than serially. For example, when you have two subsystems within a program that are fully independent of each other, consider making them into individual threads. A word of caution is in order, however. If you create too many threads, you can actually degrade the performance of your program rather than enhance it. Remember, overhead is associated with context switching; If you create too many threads, more CPU tie will be spent changing contexts than in executing your program!
Thank you for your article it's looks good and providing some valuable information
ReplyDeleteRegards,
Java Online Training
Java Online Training in India
Java Online Training India
Java Online Training in Hyderabad
Java Online Training Hyderabad
Java Training in Hyderabad
Java Training in India
Java Training Institutes in India
Java Training Institutes in Hyderabad
Java Course in Hyderabad
Java Training