Tuesday, March 10, 2009

Threading - Q&A lots of Java

Threading

1.What’s difference between thread and process?

A single process can have multiple threads that share global data and address space with other threads running in the same process, and therefore can operate on the same data set easily. Processes do not share address space and a different mechanism must be used if they are to share data.
If we consider running a word processing program to be a process, then the auto-save and spell check features that occur in the background are different threads of that process which are all operating on the same data set (your document).

--
process is a execution of a program and program contain set
of instructions but thread is a single sequence stream
within the process.thread is sometime called lightweight
process. single thread alows a os to perform singler task

ata time similarities between process and threads are:
1)share cpu.
2)sequential execution
3)create child
4)if one thread is blocked then the next will be start to
run like process.
dissimilarities:

1)threads are not independent like process.
2)all threads can access every address in the task unlike
process.
3)threads are design to assist onr another and process
might or not might be assisted on one another.


2. What is thread safety and synchronization?

By far, the most common use of thread synchronization is to ensure mutually exclusive access to a shared resource by multiple threads. In the Win32® API, the CRITICAL_SECTION structure and associated functions offers the fastest and most efficient way to synchronize threads for mutually exclusive access when the threads are all running in a single process. The Microsoft® .NET Framework doesn't expose a CRITICAL_SECTION structure, but it does offer a similar mechanism allowing mutually exclusive access among a set of threads running in the same process. This mechanism is made possible by way of SyncBlocks and the System.Threading.Monitor class.

class SomeType {
private:
// The private CRITICAL_SECTION field
// associated with each object

CRITICAL_SECTION m_csObject;

public:
SomeType() {
// The constructor initializes the
// object's CRITICAL_SECTION field
InitializeCriticalSection(&m_csObject);
}


~SomeType() {
// The destructor deletes the
// object's CRITICAL_SECTION field
DeleteCriticalSection(&m_csObject);
}

void SomeMethod() {
// In the methods, we use the object's

// CRITICAL_SECTION field to synchronize
// access to the object by multiple threads.

EnterCriticalSection(&m_csObject);
// Execute thread-safe code here...
LeaveCriticalSection(&m_csObject);

}

void AnotherMethod() {
// In the methods, we use the object's
// CRITICAL_SECTION field to synchronize
// access to the object by multiple threads.

EnterCriticalSection(&m_csObject);

// Execute thread-safe code here...
LeaveCriticalSection(&m_csObject);
}
};

class Transaction {


// Private field holding the time of
// the last transaction performed
private DateTime timeOfLastTransaction;

public void PerformTransaction() {
// Lock this object
Monitor.Enter(this);


// Perform the transaction...

// Record time of the most recent transaction
timeOfLastTransaction = DateTime.Now;

// Unlock this object
Monitor.Exit(this);
}


// Public read-only property returning
// the time of the last transaction
public DateTime LastTransaction {
get {
// Lock this object
Monitor.Enter(this);

// Save the time of the last transaction

// in a temporary variable
DateTime dt = timeOfLastTransaction;

// Unlock this object
Monitor.Exit(this);

// Return the value in the temporary variable
return(dt);

}
}
}

class Transaction {

// Private field holding the time of
// the last transaction performed

private DateTime timeOfLastTransaction;

public void PerformTransaction() {
lock (this) {
// Perform the transaction...

// Record time of the most recent transaction
timeOfLastTransaction = DateTime.Now;

}
}

// Public read-only property returning
// the time of the last transaction
public DateTime LastTransaction {
get {
lock (this) {
// Return the time of the last transaction

return timeOfLastTransaction;
}
}
}
}

What is semaphore?

A hardware or software flag.
In multitasking systems, a semaphore is a variable with a value that indicates the status of a common resource.
Its used to lock the resource that is being used.
A process needing the resource checks the semaphore to determine the resource's status
and then decides how to proceed.

In programming, especially in UNIX systems, semaphores are a technique for coordinating or synchronizing activities
in which multiple process compete for the same operating system resources.
A semaphore is a value in a designated place in operating system (or kernel) storage that each process can check and then change.
Depending on the value that is found, the process can use the resource or will find that it is already in use and must wait for some period before trying again.
Semaphones can be binary (0 or 1) or can have additional values.
Typically, a process using semaphores checks the value and then, if it using the resource, changes the value to reflect this so that subsequent semaphore users will know to wait.
Semaphores are commonly use for two purposes: to share a common memory space and to share access to files.
Semaphores are one of the techniques for interprocess communication (interprocess communication).
The C programming language provides a set of interfaces or "functions" for managing semaphores.



What are monitors?


Monitors

Like the lock keyword, monitors prevent blocks of code from simultaneous execution by multiple threads. The Enter method allows one and only one thread to proceed into the following statements; all other threads are blocked until the executing thread calls Exit. This is just like using the lock keyword. In fact, the lock keyword is implemented with the Monitor class. For example:

lock (x)
{
DoSomething();
}

This is equivalent to:

System.Object obj = (System.Object)x;
System.Threading.Monitor.Enter(obj);
try

{
DoSomething();
}
finally
{
System.Threading.Monitor.Exit(obj);
}

Using the lock keyword is generally preferred over using the Monitor class directly, both because lock is more concise, and because lock insures that the underlying monitor is released, even if the protected code throws an exception. This is accomplished with the finally keyword, which executes its associated code block regardless of whether an exception is thrown.

For more information on monitors, see Monitor Synchronization Technology Sample.



What’s the importance of synchronized blocks?
http://stackoverflow.com/questions/574240/synchronized-block-vs-synchronized-method

The only real difference is that a synchronized block can choose which object it synchronizes on. A synchronized method can only use 'this' (or the corresponding Class instance for a synchronized class method). For example, these are semantically equivalent:

synchronized void foo() {

...
}

void foo() {

synchronized (this) {

...
}
}

The latter is more flexible since it can compete for the associated lock of any object, often a member variable. It's also more granular because you could have concurrent code executing before and after the block but still within the method. Of course, you could just as easily use a synchronized method by refactoring the concurrent code into separate non-synchronized methods. Use whichever makes the code more comprehensible.



How do we create threads?

win32 api function CreateThread()
Thread

what’s the difference in using runnable and extends in threads?
JAVA
http://www.particle.kth.se/~lindsey/JavaCourse/Lectures/Lecture_7A/threads.html



Can you explain Thread.sleep?

According to the docs, calling Thread.Sleep(0) causes the thread to be
"suspended to allow other waiting threads to execute."


How to stop a thread?
Abort or let it run to stop itself

What is wait() and notify() ?

Can you explain how Scheduling and Priority works in threads?
Can you explain Yielding in threading?
what are daemon threads?

No comments: