Thread in Java is one of those
topics which always confuse beginners but given its important and strength it
provides to Java language, it's very important for every Java developer to
learn and understand the fundamental concept of multi-threading and basic
points about Thread in Java.
I had started thread
programming in Java by animating a couple of words in Applets, that was
an amazing experience to code animation, but after spending almost 10 years on
developing core Java application and I am still discovering things on threading
and concurrency. My first program which involves Thread had three words
dropping from each corner of the screen and I was excited to see that animation
driven by Java thread.
Important points about Thread in Java
java.lang.Thread
class but JVM plays an important
role of all Thread activities. Thread is used to execute task in parallel and
this task is coded inside run()
method of Runnable interface.
You can create Thread in Java programming language by either extending Thread class , implementing Runnable
or by using Executor framework in Java. Remember Runnable
doesn't represent Thread actually its a task which is
executed by Thread. Read more about extends Thread vs implements Runnable here.
1. Thread in Java represent an independent path of execution. (classic definition but I still like it). Thread is represented by
2. During its life time thread remains on various Thread states like NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING which describe what thread is doing. NEW means thread is just created but not yet stated, RUNNABLE means thread is started but waiting for CPU to be assigned by thread scheduler. BLOCKED, WAITING and TIMED_WAITING means thread is not doing anything instead its been blocked and waiting for IO to finished, class or object lock, or any other thread etc.
3. There are two kinds of Thread in Java daemon or non daemon (also called user threads). Java programs runs until there is at least one non-daemon thread exists. First non-daemon thread started by JVM is main thread which is created by JVM and responsible for executing code inside main method in Java. This is called "VM thread" in HotSpot Virtual Machine. Any thread created using java.lang.Thread start() methods from main thread is by default non-daemon but you can make it daemon by calling setDaemon(true) method. Newly created thread in Java inherits daemon property from the thread which creates it. Since main thread is non-daemon, any thread created by it by default remains non-daemon.
4. Every Java threads has priority and name. You can set priority and assign meaningful name while creating object of java.lang.Thread class in Java. its recommend practice to give every thread a meaningful name while creating it , it helps later when you debug your Java program or take thread dump for analysis. Otherwise Java will give your Thread default name like "Thread-number" if Thread is created using java.lang.Thread or "pool-number-thread-number" if Thread is created using ThreadFactory. In Java higher priority thread get preference in Execution over lower priority thread. you can check priority by using method like getProiroty() from thread class.
5.Creation of thread is a time-consuming job so having a Thread pool for performing task concurrently is modern day requirement of performance and scalability. Java 5 provides Executor framework which encapsulate task of creating and managing thread from application code. Consider using Thread pool if your application requires to handle load. In web and application server manages this thread pool because each request is processed in its own thread.
6. Thread.sleep() method is used to pause thread for specified duration. It is an overloaded method defined in java.lang.Thread class. On the other hand Thread.join() is used to wait for another thread to complete its task before running and yield() method is used to relinquish CPU so that other thread can acquire it. See difference between sleep, wait and yield for details.
7. wait() and notify() methods are used to communicate between two threads i.e. for inter thread communication in Java. Always check condition of wait() method in loop and call them from synchronized context. wait() is a method which is defined in object class, and puts the current thread on hold and also releases the monitor (lock) held by this thread, while notify() and notifyAll() methods notifies all thread waiting on that monitor. There is no guarantee which thread will picked up by thread scheduler and given CPU to execute because of of notification. To learn more about how to use wait, notify and notifyAll method to achieve inter-thread communication, see my post about solving producer consumer problem in Java using wait and notify method.
8. Thread scheduling is done by Thread Scheduler which is platform dependent and stays inside JVM. There is no known way to control thread scheduler from Java and many of thread related decision is done by scheduler like if there are many threads is waiting then which thread will be awarded CPU.
9. Thread.isActive() method is used to check whether a thread is active or not. A thread is said to be active until it has not finished either by returning from run() method normally or due to any exception. Thread.holdsLock() method is used to check if a thread holds a lock or not. See how to check if thread holds lock or not for more details.
10. Every thread in Java has its own stack, which is used to store local variables and method calls. Size of this stack can be controlled using -XX:ThreadStackSize JVM option e.g. -XX:ThreadStackSize=512.
No comments:
Post a Comment