Questions Related to Thread

Q:What’s the difference between Thread and Runnable types?
Ans: Java Thread controls the main path of execution in an application. When you invoke the Java Virtual Machine with the java command, it creates an implicit thread in which to execute the main method. The Thread class provides a mechanism for the first thread to start-up other threads to run in parallel with it.
The Runnable interface defines a type of class that can be run by a thread. The only method it requires is run, which makes the interface very easy to to fulfil by extending existing classes. A runnable class may have custom constructors and any number of other methods for configuration and manipulation.

Q: If all methods are synchronized, is a class thread safe?
Ans: Even if all the methods of a class are synchronized, it may still be vulnerable to thread safety problems if it exposes non-final fields or its methods return mutable object references that could be manipulated by multiple threads. Non-final fields should be declared private and encapsulated with synchronization. Rather than return references to internal object fields, create an independent copy that has no relation to the original, known as a deep copy.
A deep copy of an object duplicates the content and state of the original object and all its constituent fields in such a way that none of its properties refer to instances in the original at any level.
These measures will help prevent uncontrolled access to the internal state of objects, but you must also ensure synchronization techniques are applied in a robust, consistent manner that will not cause deadlock or race conditions. It is generally better to use synchronized blocks than synchronized methods for performance reasons. Limit the extent of synchronized blocks and ensure they all use the same object monitor.

Q: What's the difference between a thread's start() and run() methods?
Ans: The separate start() and run() methods in the Thread class provide two ways to create threaded programs. The start() method starts the execution of the new thread and calls the run() method. The start() method returns immediately and the new thread normally continues until the run()method returns.
The Thread class' run() method calls the run() method of the Runnable type class passed to its constructor. Subclasses of Thread should override the run() method with their own code to execute in the second thread.
Depending on the nature of your threaded program, calling the Thread run() method directly can give the same output as calling via the start() method. Howevever, the code will only be executed in a new thread if the start() method is used.

Q: What is a Runnable object and a Runnable argument?
Ans: A Runnable object is one that implements the Runnable interface, which is the type used to execute new threads. The Runnable interface only has one method, run(), which must be implemented by a Runnable class.
In Java an argument is a primitive value or object reference that is passed to a constructor or method, defined in the method signature. A Runnable argument would be a constructor or method argument that is declared to be a Runnable type. The constructor of the Thread class is the most obvious example.
public Thread(Runnable target);
This constructor requires a Runnable type argument to be passed when a Thread is instantiated.

Q: What is a green thread?
Ans: A green thread refers to a mode of operation for the Java Virtual Machine (JVM) in which all code is executed in a single operating system thread. If a Java program has any concurrent threads, the JVM manages multi-threading behaviour internally rather than use additional operating system threads.
There is a significant processing overhead for the JVM to keep track of thread states and swap between them, so green thread mode has been deprecated and removed from more recent Java implementations. Current JVM implementations make more efficient use of native operating system threads. These days there is no case where the green thread approach is useful except on systems where this is the only concurrency scheme that is available for Java, on old operating systems and hardware platforms.

Q:What are the daemon threads? 
Ans: Daemon thread are service provider threads run in the background,these not used to run the application code generally.When all user threads(non-daemon threads) complete their execution the jvm exit the application whatever may be the state of the daemon threads. Jvm does not wait for the daemon threads to complete their execution if all user threads have completed their execution.
To create Daemon thread set the daemon value of Thread using setDaemon(boolean value) method. By default all the threads created by user are user thread. To check whether a thread is a Daemon thread or a user thread use isDaemon() method.
Example of the Daemon thread is the Garbage Collector run by jvm to reclaim the unused memory by the application. The Garbage collector code runs in a Daemon thread which terminates as all the user threads are done with their execution. 

Q:What happens if a start method is not invoked and the run method is directly invoked? 
Ans: If a thread has been instantiated but not started its is said to be in new state. Unless until a start() method is invoked on the instance of the thread, it will not said to be alive. If you do not call a start() method on the newly created thread instance thread is not considered to be alive. If the start() method is not invoked and the run() method is directly called on the Thread instance, the code inside the run() method will not run in a separate new thread but it will start running in the existing thread. 

People who read this post also read :


Did you know that you can generate cash by locking special pages of your blog / website?
All you need to do is join AdWorkMedia and run their content locking tool.

You should see how my buddy Wesley Virgin's report launches in this shocking and controversial VIDEO.

Wesley was in the army-and shortly after leaving-he unveiled hidden, "mind control" secrets that the CIA and others used to obtain whatever they want.

As it turns out, these are the EXACT same tactics lots of famous people (especially those who "became famous out of nowhere") and elite business people used to become wealthy and famous.

You probably know that you utilize only 10% of your brain.

That's mostly because most of your BRAINPOWER is UNCONSCIOUS.

Perhaps this thought has even taken place INSIDE OF YOUR own head... as it did in my good friend Wesley Virgin's head about seven years ago, while riding an unlicensed, beat-up bucket of a vehicle without a driver's license and with $3.20 in his pocket.

"I'm so fed up with living check to check! Why can't I turn myself successful?"

You took part in those types of conversations, ain't it right?

Your success story is going to start. All you need is to believe in YOURSELF.

Take Action Now!

Do this hack to drop 2lb of fat in 8 hours

Over 160 000 men and women are losing weight with a simple and SECRET "liquids hack" to drop 1-2 lbs every night in their sleep.

It is proven and works with anybody.

This is how to do it yourself:

1) Get a glass and fill it half the way

2) Proceed to use this awesome hack

and be 1-2 lbs lighter in the morning!

Post a Comment


Twitter Delicious Facebook Digg Stumbleupon Favorites More