Sunteți pe pagina 1din 3

(B) What’s difference between thread and process?

A thread is a path of execution that run on CPU, a process is a collection of threads that
share the same virtual memory. A process has at least one thread of execution, and a
thread always run in a process context. A process is a collection of virtual memory space,
code, data, and system resources. A thread is code that is to be serially executed within a
process. A processor executes threads, not processes. One more significant difference
between process and thread is every process has its own data memory location but all
related threads can share same data memory and have their own individual stacks. Thread
is a light weighted process, collection of threads become process.

(B) What is thread safety and synchronization?


Thread safe is used to describe a method that can run safely in multithreaded
environments
allowing accessing to data in a safe and efficient way. Thread safety is achieved by
synchronization. Synchronization assures that the object data is not accessed by multiple
threads at the same time.

(I) What is semaphore?


Semaphore is an object which helps two threads to communicate with one another in
order to synchronize there operation.

(I) What are monitors?


Monitor is a body of code which can be executed by only one thread at a time. If any
other thread tries to get access at the same time, it will be suspended until the current
thread releases the Monitor.

(B) What’s the importance of synchronized blocks?


Synchronized blocks implement the concept of monitors described in the first answer.
Below is the code snippet of the synchronized block.

(B) How do we create threads?


There are two ways to create a thread.
extend the java.lang.Thread class.
Create a class which extends the thread class. When the class is instantiated, the thread
and object are created together and the object is automatically bound to the thread. By
calling the object's start() method, the thread is started and immediately calls the object's
run() method.
implement the java.lang.Runnable interface
Create the thread and supply it an object with a run() method. This object will be
permanently associated with the thread. The object's run() method will be invoked when
the thread is started. This method of thread creation is useful if you want many threads
sharing an object.

(I) what’s the difference in using runnable and extends in threads?


Below are some of the main differences between threads using runnable interface and by
extending the thread class.
v Thread is a class and Runnable is an interface in java.
v If a class is not extending another class we can use either extending Thread class or
implementing Runnable interface. If our class is already extending another class we
can't extend Thread class because java doesn't support multiple inheritance so we
have left only one choice that is implementing Runnable interface.
v When you extend a thread each of your threads has a unique object associated with
it, whereas with Runnable, many threads share the same object instance.

(B) Can you explain Thread.sleep?


Thread.sleep() is a static method of the Thread class that causes the currently executing
thread to delay for a specified number of milliseconds. Below is the code snippet:-
try
{
Thread.sleep ( 1000 );
}
catch ( InterruptedException e ) {}

(B) How to stop a thread?


thread.stop;

(I) What is wait() and notify() ?


By using wait() and notify(), a thread can give up its hold on a lock at an arbitrary point,
and then wait for another thread to give it back before continuing. By executing wait()
from a synchronized block, a thread gives up its hold on the lock and goes to sleep.Later,
when the necessary event happens, the thread that is running it calls notify() from a block
synchronized on the same object. Now the first thread wakes up and begins trying to
acquire the lock again.

(I) Can you explain how Scheduling and Priority works in threads?
Every thread has priority.If, at any time, a thread of a higher priority than the current
thread becomes runnable, it preempts the lower priority thread and begins executing. By
default, threads at the same priority are scheduled round robin, which means once a
thread starts to run
In order to set thread priority below is the code snippet:-
Thread mythread = new MyThread("my");
mythread.setPriority( Thread.NORM_PRIORITY + 1 );
mythread.start();
MIN_PRIORITY :- Minimum priority
NORM_PRIORITY :- Normal priority
MAX_PRIORITY :- Maximum priority

(B) Can you explain Yielding in threading?


It causes the currently executing thread object to temporarily pause and allow other
threads
to execute.
(I) what are daemon threads?
Daemon threads are designed run in background. One example of a daemon thread is the
garbage collector thread. You can use setDaemon() to mark a thread as daemon.

S-ar putea să vă placă și